0

ネストされたモデルセットがあります:

class Event < ActiveRecord::Base  
    belongs_to :place
      :place
    attr_accessible :place_attributes, :reject_if => :all_blank, :allow_destroy => false

class Place < ActiveRecord::Base
    has_many :events
    validates :label, :presence => true, 
        :uniqueness => {:case_sensitive => true, :on => :create }
    validates :description, :presence => {:on => :create}, 
        :uniqueness => {:case_sensitive => true , :on => :create}

テストシナリオでは、ネストされたフォームでは、ユーザーはPlace#label属性のみを更新でき、他のすべての情報は保持されます。

test "should_update_event_place_data" do
    put :update, :locale => I18n.locale, :id => @event[:id],
      :event => { :place_attributes => { label: "a very beautiful place" } }

これにより、EventsController#updateへのリクエストが発生し、パラメーターを受け取ります。

params
    {"event"=>{"place_attributes"=>{"label"=>"a very beautiful place"}}, "locale"=>"en",
    "id"=>"145", "controller"=>"backoffice/events", "action"=>"update"}

(rdb:1)  @event.update_attributes(params[:event])
 false
@messages={:"place.description"=>["cannot be blank"]

しかし、検証は更新ではなく作成時に行われます....検証エラーは検出されません..何が間違っている可能性がありますか?

手伝ってくれてありがとう

I did more testing 
debugger , right after the test setup ( before sending the put request)
@event_0
#<Event id: 161, account_id: 3, place_id: 249, slug: "my-new-event-on-2013-01-01-at-    edinburgh-united-king...", title: "My New Event"
 @event_0.place
#<Place id: 249, label: "new fake place",..

test request:
put :update, :locale => I18n.locale, :id => @event_0[:id], :event => { :place_attributes => {  label: "a very beautiful place"} }

params in request are OK, @request/method = PUT

In EventsController#update
@event.update_attributes(params[:event])
.... I inserted a debug in the Place model... 
(before_validation :i_am_on_create, :on => :create)
  def i_am_on_create
    debugger
    p "CREATING"
  end

 and it's creating !! don't understand why it's not updating the parent nested model
4

3 に答える 3

1

update_attributes は、更新を関連付けに伝達しません。ソース コード (http://apidock.com/rails/ActiveRecord/Base/update_attributes) を見ると、最後に #save が呼び出されていることがわかります。そして、これはデフォルトの動作です:

# existing resource 'mazeratti car' 
car.name = "Wheelz"
car.brand.label = "Ferrari"
car.save
car.reload
car.name #=> "Wheelz"
car.brand.label #=> "Mazeratti"

オブジェクトが更新されるたびに関連付けを更新する場合は、「自動保存」の使用を検討してください (http://apidock.com/rails/ActiveRecord/Associations/ClassMethods/belongs_to : オプション)

于 2012-12-04T15:54:23.390 に答える
0

ラベル属性が更新されていることだけをテストしたい場合は、「イベント」全体ではなく、そのフィールドのみでupdate_attributeを実行してみませんか?何かのようなもの:

@event.place_attributes.update_attribute(
      :label => params[:event][:place_attributes][:label]
)

テストされていません-しかし、あなたはアイデアを得る...

于 2012-12-04T12:29:03.947 に答える
0

解決した

ネストされたモデルを更新するには、モデル インスタンス id を追加する必要があります。

 put :update, :locale => I18n.locale, :id => @event_0[:id], :event => { :place_attributes => { id: @event_0.place[:id],  label: "a very beautiful place"} }

そのため、 :place_attributes に既存の @event_0.place[:id] を追加し、現在更新中です

2 月 17 日 17:04 の下部ページの accept_nested_attributes_for with find_or_create?の Anson の回答で見つけました。

于 2012-12-04T15:51:41.870 に答える