2014-01-23 71 views
0

我跟着这LINKhas_many:通过不保存协会

但它不保存关联。

我的模型是:

class Shift < ActiveRecord::Base 
     has_many :week_shifts, :dependent => :destroy 
     has_many :weeks, :through => :week_shifts, :dependent => :destroy  
     accepts_nested_attributes_for :weeks 
    end 

    class Week < ActiveRecord::Base 
     has_many :week_shifts, :dependent => :destroy 
     has_many :shifts, through: :week_shifts, :dependent => :destroy 
    end 

    class WeekShift < ActiveRecord::Base 
     belongs_to :week 
     belongs_to :shift 
    end 

和_form是:

<% @weeks = Week.find(:all) %> 
    <% @weeks.each do |week| %> 
    <div class="field"> 
    <%= check_box_tag 'week_ids[]', week.id, @shift.weeks.include?(week) %> 
    <%= week.day %> 
    </div> 
    <% end %> 
    <%= hidden_field_tag 'week_ids[]', '' %> 

输出是这个

渲染偏移/布局内edit.html.erb /应用( 109.3ms) 在119ms内完成200 OK(查看:44.4ms | ActiveRecord:73.0ms) 启动PATCH“/移位/ 3 “为127.8.96.129于2014-01-23 06:46:58 -0500通过ShiftsController处理#更新为HTML参数:{”utf8“=>”✓“, ”authenticity_token“=>”Ltd3Ln5YJcRf40wQiPeGC + rRVeeGTpU + X1pUGjxbN6M =“, ”shift“=> {”cod“=>”CN“,”nome“=>”Centrale Notte“,”descr“=>”Operatore Centrale Notte“,”stato“=>”1“ “inizio(4i)”=>“00”,“inizio(5i)”=>“00”, “fine(4i)”=>“08” “week_ids”=> [“1”,“2”,“3”,“4”, “”],“commit”=>“Update Shift”,“id”=>“3”}

回答

0

看起来您需要限制您的表单的输入为week_ids,以便它们构成shift PARAMS的一部分。目前,它的发布是这样的:

{ 
    "shift"=>{ 
    "cod"=>"CN", 
    "nome"=>"Centrale Notte", 
    "descr"=>"Operatore Centrale Notte", 
    "stato"=>"1", 
    "inizio(4i)"=>"00", 
    "inizio(5i)"=>"00", 
    "fine(4i)"=>"08", 
    "fine(5i)"=>"00" 
    }, 
    "week_ids"=>["1", "2", "3", "4", ""] 
} 

而应该是这样的:

{ 
    "shift"=>{ 
    "cod"=>"CN", 
    "nome"=>"Centrale Notte", 
    "descr"=>"Operatore Centrale Notte", 
    "stato"=>"1", 
    "inizio(4i)"=>"00", 
    "inizio(5i)"=>"00", 
    "fine(4i)"=>"08", 
    "fine(5i)"=>"00", 
    "week_ids"=>["1", "2", "3", "4", ""] 
    } 
} 

所以你的形式,应该有这个代替:

<%= check_box_tag 'shift[week_ids][]', week.id, @shift.weeks.include?(week) %>