I
I
Ivan Nesterovich2014-01-30 22:17:34
Ruby on Rails
Ivan Nesterovich, 2014-01-30 22:17:34

Why doesn't Ruby on Rails fields_for work?

There are 3 models: Products, Orders, Linking model
Product:

has_many :purchaseds, :dependent => :destroy
has_many :orders, :through => :purchaseds

order
has_many :purchaseds, :dependent => :destroy
has_many :products, through: :purchaseds
accepts_nested_attributes_for :purchaseds

Purchased
belongs_to :product
belongs_to :order

In the controller I write:
@order = Order.new()
@products.each do |p|
   @order.purchaseds.build({
        :product_id => p.id
   })
end

As a result, my form is empty!
= form_for(@order) do |f|
  - f.fields_for :purchaseds do |p|
     = debug p

= debug @order.purchaseds outputs an array of objects as expected.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Nesterovich, 2014-01-30
@vanderv

Found an error

= form_for(@order) do |f|
  = f.fields_for :purchaseds do |p|
     = debug p

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question