2017-01-05 9 views
1

Pls私はここで助けが必要です。 私は2つのモデルがあります: - due_jobとoutgoing_job due_job has_many outgoing_jobs outgoing_job belongs_to due_job。 ユーザーのoutgoing_jobを更新して、同時に他のユーザーのdue_jobを作成しようとしています。 マイモデル:レールで1つのフォームを使用して別のモデルを作成中にモデルを更新する方法

class DueJob < ActiveRecord::Base 

belongs_to :user 
has_many :outgoing_jobs 
accepts_nested_attributes_for :outgoing_jobs 

class OutgoingJob < ActiveRecord::Base 

belongs_to :user 
belongs_to :outgoing_jobs 
accepts_nested_attributes_for :outgoing_jobs 

コントローラ:

class OutgoingJobsController < ApplicationController 

def index 
    @outgoing_job = OutgoingJob.new 
    @outgoing_jobs = OutgoingJob.all 
end 

def new 
    @outgoing_job = OutgoingJob.new 

end 

def create 
    @outgoing_job = OutgoingJob.new(outgoing_job_params) 
    respond_to do |format| 

     if @outgoing_job.save 


      flash.now[:success] = "saved" 
      format.html {redirect_to current_user} 
      format.json {render json: @outgoing_job, status: :created, location: @outgoing_job} 
     else 
      flash[:danger] = "not saved" 
      format.html {redirect_to root_path} 
      format.json {render json: @outgoing_job.errors, status: :unprocessable_entity } 
     end 
    end 
end 

def show 
@outgoing_job = OutgoingJob.find(params[:id]) 
end 

def update 
    @outgoing_job = OutgoingJob.find(params[:id]) 

respond_to do |format| 
    if @outgoing_job.update(outgoing_job_params) 
     format.html { redirect_to '/users/outgoing_job_dashboard', 
     notice: 'job updated' } 
     format.json {render action: 'show', 
      status: :ok, location: @outgoing_job } 
    else 
     format.html { render action: 'edit'} 
     format.json { render json: @outgoing_job.errors, 
      status: :unprocessable_entity} 
    end 
end 
end 

def destroy 
    @outgoing_job.destroy 
respond_to do |format| 
    format.html {redirect_to current_user} 
    format.json { head :no_content} 
end 
end 



private 
def outgoing_job_params 
    params.require(:outgoing_job).permit(:outgoing_job_value, 
     :sent, 
     :confirmed, 
     :done, 
     :due_job_id, 
     :user_id) 
end 
end 

due_jobsためのコントローラは、本質的に同じです。私は私の見解では、これを行うとき

しかし、:

<% OutgoingJob.all.each do |od| %> 
    <table class="table table-striped table-responsive"> 
    <thead> 
     <tr> 
      <th>ID</th> 
      <th>Done By</th> 
      <th>Done for</th> 
      <th>Beneficiary</th> 
      <th>Amount proposed</th> 
      <th>Amount to paid</th> 
      <th>Create due job</th> 
      <th>Actions</th> 
     </tr> 
    </thead> 



    <% if (od.confirmed == true) && (od.done== false) %> 
     <tbody> 
      <tr> 
       <td><%= od.id %></td> 
       <td><%= od.user.first_name %> <%= od.user.last_name %></td> 
       <td><%= od.due_job.user.first_name %> <%= od.due_job.user.last_name %></td> 
       <td><%= od.due_job.user.user_detail %></td> 
       <td>$ <%= number_with_delimiter(od.outgoing_job_value, delimiter: ',') %> </td> 
       <td> <%= --- %> </td> 

       <td> 
       <%= simple_form_for (DueJob.new) do |u| %> 
       <%= u.hidden_field :due_job_value, value: od.outgoing_job_value %> 
       <%= u.hidden_field :user_id, value: od.user.id %> 
       <%= u.fields_for od do |f| %> 
       <%= f.hidden_field :done, value: true %> 
       <%end%> 
       <%= u.submit "create", class: "btn btn-success" %> 

       <%end%> 
       </td> 
       <td><%= link_to "View", od %></td> 
      </tr> 
     </tbody> 


     <%end%> 
    </table> 
    ..... 

ネストされたフォームを使用して、私はDueJobカントーのための新しいレコードを作成することができていますが、それはoutgoing_jobを更新doesntの。私は何が欠けているのですか?

+0

コードをクリーンアップすれば、より良い運を得られるかもしれません。 'if(od.confirmed == true)&&(od.done == false)'は次のようになります: 'od.confirmed? &! od.done? '。 '<%end%>'のようなチャンクは、より明確に書かれるべきです: '<% end %>'。そして、すべてのものの愛のために聖なる、一貫した刻み目を使用して、言葉のスープを見ていないようにしてください。 – coreyward

+0

ありがとう@coreyward。 Dulyは指摘する。私は自分のベストを尽くす。 –

答えて

2

ActiveRecord callbacksを使用してコードの一部をモデルに格下げすることをお勧めします(私は、1つのビューからすべてを実行しようとするとその方法が間違っているとは思っていません)。 DueJobのためのあなたのモデルで

のようなものを追加:ちょうどあなたのソースコードを読んでから

class DueJob < ActiveRecord::Base 
    # Register a callback to execute every time you create a new DueJob 
    after_create :update_done_flag_on_outgoing_job 

    # ... and add some code for that callback. 
    def update_done_flag_on_outgoing_job 
    outgoing_job.update_attribute :done, true 
    end 

を、私はあなたが欲しい、新しく作成されたDueJob間の接続や特定のOutgoingJobレコードを識別方法を理解するために苦労しました。更新する。 @coreywardのように、あなたのコードをもう少しきれいに提示できれば助けになるでしょう。私の例のスニペットとして使用できない場合は、いつでもupdate_done_flag_on_outgoing_jobメソッドを必要に応じて適応させることができます。

あなたが役に立ったら嬉しいです。

関連する問題