2017-01-05 64 views
1

請在這裏獲得幫助。 我有2個模型: - due_job和outgoing_job due_job has_many outgoing_jobs outgoing_job belongs_to due_job。 我試圖更新用戶的outgoing_job,同時爲另一個用戶創建due_job。 我的模型:如何在使用軌道中的一個窗體創建另一個模型時更新模型

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。我錯過了什麼?

+0

如果清理代碼,你可能會有更好的運氣。像這樣:'if(od.confirmed == true)&&(od.done == false)'應該看起來更像這樣:'如果od.confirmed? &! od.done?'。像'<%end%>'這樣的塊應該更清楚地寫成:'<% end %>'。並且爲了聖潔的一切的愛,應該使用一致的縮進,所以我們不看字湯。 – coreyward

+0

謝謝@coreyward。正式指出。我會盡力。 –

回答

2

我建議你利用ActiveRecord callbacks將一些代碼降級到你的模型(我懷疑試圖從一個視圖去做所有事情都是要走的路)。

在你的模型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方法。

希望你覺得有幫助。

相關問題