2013-05-17 39 views
0

你好我的同伴!红宝石 - 通过输入值嵌套对象

我试图做到的,是通过订单形式有两种方式编译的系统:

  1. 通过fullfilling自己的属性(:SENDER_NAME,:sender_mobile等)
  2. 通过通过附件上的价格标签选择产品。

经过一段时间的戳动,我设法在订单上显示产品列表。在这里,3款和意见

型号/ order.rb

class Order < ActiveRecord::Base 
    attr_accessible :sender_comment, :sender_email, :sender_mobile, :sender_name, :order_attributes 

    has_many :products 

    accepts_nested_attributes_for :products 
end 

型号/ product.rb

class Product < ActiveRecord::Base 
    belongs_to :order 
    attr_accessible :product_name, :product_description, :prices_attributes, :order_id 

    has_many :prices 

    accepts_nested_attributes_for :prices 
end 

型号/ price.rb

class Price < ActiveRecord::Base 
    belongs_to :product 

    attr_accessible :product_id, :price_label, :price_amount, :price_checked, :how_many_prices, :products_attributes 
end 

的意见/订单/_form.html.erb

<%= form_for(@order) do |f| %> 
    <div class="field"> 
    <%= f.label :sender_name %><br /> 
    <%= f.text_field :sender_name %> 
    </div> 

    # [...] other order's fields... 

    <%= f.fields_for :product do |builder| %> 
    <%= render "products_field", :f => builder %> 
    <% end %> 

    <div class="actions"> 
    <%= f.submit %> 
    </div> 
<% end %> 

的意见/命令/ _products.html.erb

<% @products.each do |p| %> 
    <td><%= p.product_name %></td> 
<td><%= p.product_description %></td><br /> 
     <% p.prices.each do |price| %> 
      <td><%= price.price_label %></td><br /> 
      <td><%= price.price_amount %></td><br /> 
      <td><input type="radio" class="order_bool" name="<%= p.product_name %>" <% if price.price_checked == true; puts "SELECTED"; end %> value="<%= price.price_amount%>"/></td><br /> 
     <% end %> 
<% end %> 

产品和亲戚价格印在了订单,但一旦选定他们没有保存为order_attributes;以及订单的属性,每个选中的广播都会生成类似的对象

[#<Product id: nil, order_id: 23, product_name: nil, product_description: nil, created_at: nil, updated_at: nil>] 

如何将选定的产品转换为有效的order_attributes?

这是我第一个使用OOP的项目,而且我自己都在学习,只有很少的帮助,但是来自互联网。请不要太苛刻!

如果您觉得不够合适,也可随意更改标题;英语不是我的母语,我很难用简单的几句话来回顾这个问题。

感谢您的耐心:)

+0

你到底想存储为了属性?您可以随时通过order_1.products访问特定订单的产品(order_1是Order的一个实例)。这将按顺序为您提供所有产品。 –

回答

0

基本上你目前_products_fields部分甚至没有连接到form对象。您正在构建任意输入。见form_for documentationfields_for documentationradio_button documentation

注意:当您使用f.method_name你究竟要调用该方法的FormBuilder versions而不是FormHelper版本。由于FormHelper版本具有更多且仍然相关的文档,因此是更好的版本。只需省略object_name参数即可。

我想改变这些行应该做的伎俩:

表部分:

<%= f.fields_for :products, @products do |builder| %> 
    <%= render "products_field", :f => builder %> 
<% end %> 

产品领域的部分

# 'f' is the variable passed in using ':f => builder' 
# So 'f' = builder 

# f.object is accessing the object were building the fields for 
<td><%= f.object.product_name %></td> 
<td><%= f.object.product_description %></td><br /> 
<% f.object.prices.each do |price| %> 
    <td><%= price.price_label %></td><br /> 
    <td><%= price.price_amount %></td><br /> 
    <td><%= f.radio_box("price", price.price_amount %></td><br /> 
<% end %> 
+0

- > Azolo似乎每种方法都是将方法中的应用程序带出现场;它保存选定的对象,但它远离主要的utf对象:我可以从参数中看到。我怎么可以在“新”方法中声明@product变量,以便我知道如果收音机是真的,它将通过正确的散列?目前,它需要product_attributes并且看到它们的id。但是他并不认为它是自己的价格嵌套属性;我敢肯定,这在控制器中很愚蠢。我最终可以在下午吗? –