2010-07-06 14 views
1

我試圖從窗體更新一些嵌套參數。我可以看到,從窗體中獲取的參數是正確的,但是數據庫不會被更新。更新嵌套參數不工作..最新錯誤?

視圖

<% 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 Itemgroup < ActiveRecord::Base 
    belongs_to :order 
    has_many :items, :dependent => :destroy 
    has_one :kind 

    accepts_nested_attributes_for :items, :kind 
end 

Order類

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 

控制器的重要組成部分。

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 

回答

-1

固定的問題!

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 

validates_associated行被刪除。然後它工作

1

變化

<% 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 %> 

爲編輯

<% 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 %> 

,並檢查

+0

這將給編譯錯誤,因爲fff.check_box是在循環之外,定義fff – Flexo 2010-07-06 09:53:54

+0

@Flexo: - ooohhh對不起,我編輯它,請再次檢查 – Salil 2010-07-06 10:02:01

+0

這也不工作。 我需要循環,因爲我想複製與每個複選框關聯的文本和pricefield – Flexo 2010-07-06 10:06:36