我有一个嵌套模型集:
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