Hello my fellow companion!
What I'm trying to achieve is a system by which an Order form is compiled in two ways:
- by fullfilling its own attributes (:sender_name, :sender_mobile etc..)
- 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 :)