0

Hello my fellow companion!

What I'm trying to achieve is a system by which an Order form is compiled in two ways:

  1. by fullfilling its own attributes (:sender_name, :sender_mobile etc..)
  2. by selecting products through the price labels attached on them.

After a while of poking here and there, I managed to display the product list on the order form. Here the 3 models and the views

models/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 

models/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

models/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

views/orders/_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 %>

views/orders/_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 %>

Products and relatives prices are printed in the Order form, yet once selected they're not saved as order_attributes; along with the order's attributes, every radio selected generates an object like this

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

How can I convert the selected products into effective order_attributes?

This is my first project with OOP and that I'm learning all by myself, with a very few help but from the internet. Please don't be too harsh!

Also feel free to change the title if you don't consider it appropriate enough; english is not my native language and i find very difficult to recap this issue in just a few words.

Thanks for the patient :)

4

1 回答 1

0

基本上你当前的_products_fields部分甚至没有附加到form对象上。您现在正在构建任意输入。请参阅form_for文档fields_for文档radio_button文档

注意:当您使用时,f.method_name您实际上将调用方法的FormBuilder版本而不是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 %>
于 2013-05-18T07:32:20.030 回答