This works
#controller: products action: new file:_form.erb
<% for color in Color.all %>
<div>
<%= check_box_tag "product[color_ids]", color.id,
@product.colors.include?(color) %>
<%= color.name %>
</div>
<% end %>
but if I add one more field the value will not work.
Asking a question so vague - how can I get values from database - is
very difficult to answer. Please give more details of what you are
trying to do.
Since this seems fairly basic stuff have you worked through the Rails
Guides and some tutorials? railstutorial.org is good and free to use
online.
I have 3 tables/models products, colors, stock, the problem is I need
to save product with N colors and each color I need to tell what the
quantity each color.
class Stock < ActiveRecord::Base
belongs_to :product
belongs_to :color
end
Color Model
class Color < ActiveRecord::Base
has_many :stocks
has_many :product, :through => :stocks
end
From view _form.erb
Just the associations:
<%= fields_for :stocks do |stock_form| %>
<% for color in Color.all %>
<%= stock_form.check_box :color_id %> <%= color.name %>
<%= stock_form.text_field :qtd %>
<% end %>
<% end %>