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

アイテムグループ クラス

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

  accepts_nested_attributes_for :items, :kind
end

注文クラス

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
4

2 に答える 2

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

そしてチェック

于 2010-07-06T09:49:57.757 に答える
-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行が削除されました。それからそれは働いた

于 2010-07-06T10:36:30.703 に答える