views:

24

answers:

2

Im trying to update some nested params from a form. I can see that the parameters im getting from the form is correct, however the database dont get updated.

the view

<% form_for @order do |f| %>
  <% f.fields_for :itemgroups do |ff, i| %>
    <% ff.fields_for :items do |fff| %>
      <%= ff.text_field :text, :id => "textField", :disabled => true %>
      <%= ff.text_field :price, :class => "priceField", :disabled => true %>
      <%= fff.check_box :registered, :class => i %>
    <% end %>
  <% end %>
  <%= submit_tag 'Save', :disabled_with => "Saving..." %>
<% end %>

Itemgroup class

class Itemgroup < ActiveRecord::Base
  belongs_to :order
  has_many :items, :dependent => :destroy
  has_one :kind

  accepts_nested_attributes_for :items, :kind
end

Order class

class Order < ActiveRecord::Base
  has_many :itemgroups, :dependent => :destroy
  has_many :items, :through => :itemgroups, :dependent => :destroy
  has_many :kinds, :through => :itemgroups

  accepts_nested_attributes_for :itemgroups, :allow_destroy => true

  validates_associated :itemgroups, :items ,:kinds
end

The important part of the controller.

def update
  @order = Order.find(params[:id])

  if @order.update_attributes(params[:order])
    flash[:notice] = 'Order was successfully edited.'
    redirect_to(@order)
  else
    flash[:notice] = 'An error occured.'
    render(:action => :edit)
  end
end
A: 

Change

<% f.fields_for :itemgroups do |ff, i| %>
  <% ff.fields_for :items do |fff| %>
    <%= ff.text_field :text, :id => "textField", :disabled => true %>
    <%= ff.text_field :price, :class => "priceField", :disabled => true %>
    <%= fff.check_box :registered, :class => i %>
  <% end %>

To EDITED

<% f.fields_for :itemgroups do |ff, i| %>
    <%= ff.text_field :text, :id => "textField", :disabled => true %>
    <%= ff.text_field :price, :class => "priceField", :disabled => true %>
  <% ff.fields_for :items do |fff| %>
    <%= fff.check_box :registered, :class => i %>
  <% end %>

and check

Salil
that will give compile error since the fff.check_box is outside the loop that defines fff
Flexo
@Flexo :- ooohhh sorry i EDIT it please check again
Salil
That doesnt work either.I need the loop to be as it is, because i want a duplicate of the text and pricefield associated with each checkbox
Flexo
A: 

Fixed the problem!

class Order < ActiveRecord::Base
  has_many :itemgroups, :dependent => :destroy
  has_many :items, :through => :itemgroups, :dependent => :destroy
  has_many :kinds, :through => :itemgroups

  accepts_nested_attributes_for :itemgroups, :allow_destroy => true

  # validates_associated :itemgroups, :items ,:kinds
end

the validates_associated line was removed. Then it worked

Flexo