2013-05-17 41 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。但是他並不認爲它是自己的價格嵌套屬性;我敢肯定,這在控制器中很愚蠢。我最終可以在下午嗎? –