【问题标题】:Rails 3.2: multiple nested data from one modelRails 3.2:来自一个模型的多个嵌套数据
【发布时间】:2012-11-05 11:38:02
【问题描述】:

如果出现以下情况,如何为多个嵌套属性创建表单和操作:

订单项:

has_many :item_options, :dependent => :destroy
has_many :product_options, :through => :item_options

产品选项:

belongs_to :product
belongs_to :option
has_many :item_options
has_many :line_items, :through => :item_options

项目选项:

attr_accessible :line_item_id, :product_option_id  
belongs_to :line_item, :foreign_key => "line_item_id"
belongs_to :product_option,:foreign_key => "product_option_id"

当我创建新的 LineItem 时,我需要创建新的 ItemOption(s)。这是我的表格:

        <%= form_for(LineItem.new) do |f| %>
        <%= f.hidden_field :product_id, value: @product.id %>
        <%= f.fields_for :item_options do |io| %>
            <% @product.options.uniq.each do |o| %>
              <%= o.name %>: 
              <%= io.collection_select :product_option_id, o.product_options.where(:product_id => @product.id), :id, :value %>
            <% end %>
        <%= f.submit %>
        <% end %>

当我点击添加到购物车时,我得到:

ItemOption(#70296453751440) 预期,得到 Array(#70296430421140)

将accepts_nested_attributes_for :item_options 添加到LineItem 时,我的选择未显示:(

<%= select_tag "product_option_id", options_from_collection_for_select(o.product_options.where(:product_id => @product.id), :id, :value) %>
#item_options not created:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/WM5/MqPn1yCxjKWoJQmjfko2pR4RiYV0S2KeTTpA3w=", "line_item"=>{"product_id"=>"1"}, "product_option_id"=>"5", "commit"=>"add"}

最后一个,我创建了这样的操作:

@line_item = LineItem.new(params[:line_item])
@line_item.item_options.build 
....

我哪里错了? :( 我完全糊涂了。 附言。类似问题Rails 3.2 has_many through form submission 这是表格:

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    看起来这一行:

    Parameters: {"utf8"=>"✓", "authenticity_token"=>"/WM5/MqPn1yCxjKWoJQmjfko2pR4RiYV0S2KeTTpA3w=", "line_item"=>{"product_id"=>"1"}, "product_option_id"=>"5", "commit"=>"add"}
    

    参数product_option_idline_item 哈希之外,将在里面。也许你需要这样写选择:

    <%= select_tag "line_item[product_option_id]", options_from_collection_for_select(o.product_options.where(:product_id => @product.id), :id, :value) %>
    

    我不确定,但也许是这样。也许我需要更多信息,比如失败的确切线路。


    另外,:foreign_key =&gt; "line_item_id":foreign_key =&gt; "product_option_id" 不是必需的,因为belongs_to 型号名称相同,将使用这些foreign_key。来自api

    指定用于关联的外键。默认情况下这是 猜测是带有“_id”后缀的关联名称。所以 定义 **belongs_to :person** 关联的类将使用 “person_id”作为默认的:foreign_key。同样,belongs_to :favorite_person, :class_name => "Person" 将使用外键 “favorite_person_id”。


    编辑

    抱歉,unknown attribute: product_option_id 是因为属性名称是 product_option_ids,并且是一个数组,而不是唯一值。对于has_many 关系,column 名称为 collection_singular_ids,选择应为:

    <%= select_tag "line_item[product_option_ids][]", options_from_collection_for_select(o.product_options.where(:product_id => @product.id), :id, :value) %>
    

    这应该可行,我认为 :)...

    【讨论】:

    • Tnx,但 unknown attribute: product_option_id ... 在参数中:{"utf8"=&gt;"✓", "authenticity_token"=&gt;"ik5ad6KsTOD+b4e9ioenFqfXY1nojXdEGAeNAXlCN8E=", "line_item"=&gt;{"product_id"=&gt;"1", "product_option_id"=&gt;"5"}, "commit"=&gt;"Добавить в корзину"}
    • 也许是这样的:&lt;%= select_tag "item_option[product_option_id]", options_from_collection_for_select(o.product_options.where(:product_id =&gt; @product.id), :id, :value) %&gt; 但它只是一个 item_option,甚至有几个形式:( 参数:"line_item"=&gt;{"product_id"=&gt;"1"}, "item_option"=&gt;{"product_option_id"=&gt;"4"},
    • 这个变体更好:&lt;%= select_tag "item_option[][product_option_id]", options_from_collection_for_select(o.product_options.where(:product_id =&gt; @product.id), :id, :value) %&gt;,而不是参数中的:"line_item"=&gt;{"product_id"=&gt;"1"}, "item_option"=&gt;[{"product_option_id"=&gt;"1"}, {"product_option_id"=&gt;"4"}]。但仍然没有保存(
    • 您的attr_accessible? 中有该列,例如attr_accessible :product_option_idsattr_accesible :product_option_id编辑,迟到:P
    猜你喜欢
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    相关资源
    最近更新 更多