嵌套表单在params中返回空哈希

时间:2014-12-09 09:29:27

标签: ruby-on-rails activerecord nested-forms nested-attributes nested-form-for

嗨这很奇怪我有3个模特

class Deal < ActiveRecord::Base
  has_many :deal_items, :dependent => :destroy
  accepts_nested_attributes_for :deal_items 
  attr_accessible :deal_items_attributes

end
class DealItem < ActiveRecord::Base
  self.table_name = "deals_menu_items"
  has_many :swap_items, :dependent => :destroy
  belongs_to :deal
  has_many :deal_menu_prices

  has_many :menu_items, through: :deal_menu_prices
  belongs_to :item_price

  accepts_nested_attributes_for :deal_menu_prices
  attr_accessible :deal_menu_prices_attributes
end

class DealMenuPrice < ActiveRecord::Base
  belongs_to :menu_item
  belongs_to :deal_item
  belongs_to :item_price
  attr_accessible :item_price_id, :deal_item_id, :menu_item_id, :included
end

我有查看模板

<%= nested_form_for @deal do |f| %>
  <%= f.fields_for :deal_items do |d| %>
    <%= d.fields_for :deal_menu_prices do |dm| %>
       <%= dm.select :item_price_id, :options_for_select(my_options) %>
    <% end %>
  <% end %>
<% end %> #deal form end

但是在我的控制器中,我把所有东西都弄空了,例如

"deal"=>{"deal_items_attributes"=>{"0"=>{}}, "user_id"=>4}

我做错了什么,并且在deal_items_attributes哈希中没有deal_menu_prices_attributes。我期待这样的事情

"deal"=>{"deal_items_attributes"=>{"0"=>{"deal_menu_prices_attributes"=>{item_price_id: 1} }}, "user_id"=>4}

0 个答案:

没有答案