Rails模式accepted_nested_attributes_for正常工作
我有这样的模型:
class User < ActiveRecord::Base has_one :business end class Business lambda { |a| a[:location].blank?} atr_accessible :name, :locations_attributes ... end class Location < ActiveRecord::Base belongs_to :business ... end
当我在表单中填写地址,并将表单发布到BusinessesController的create
操作时,日志显示参数是正确的:
... "business"=>{"name"=>"sos","locations_attributes"=>{"0"=>{"address"=>"location1"}, "1"=>{"address"=>"location2"}}} ...
在BusinessesController的create
操作中
# :Post /usres/1/businesses def create @user = User.find(params[:user_id]) @business = @user.build_business(params[:business]) if @business.save ... else ... end end
我检查日志,发现@business.save
没有向数据库插入任何有关locaitons的信息,但只有关于业务的信息,但是params[:business]
显然包含了hash的位置,所以我错了?
我想在哪里出错是在reject_if检查,
accepts_nested_attributes_for :locations, :reject_if => lambda { |a| a[:location].blank?}
您在位置表中的位置属性在哪里? 据我所知,你应该检查以下方式
accepts_nested_attributes_for :locations, :reject_if => lambda { |a| a[:address].blank?}