1
我遇到的麻烦,同时节省simple_form.fields_for - 禁止属性错误加载ActiveModel的Rails :: ForbiddenAttributesError - ::加载ActiveModelвForbiddenAttributesError simple_form nested_fields
在预订控制器'创建' 动作看起来如此:
def create
...
new_params = params[:booking]
new_params[:user_attributes] = new_params[:user_attributes].merge({"password"=>"osmsmsmsm32"}) # password is temp stuff to bypass User save validation
@booking = Booking.new
@booking.update(params)
# however @booking.user.update(params[:booking][:user_attributes]) gives the same error
...
end
...
def booking_params
params.require(:booking).permit(:arrived_at, :departured_at, :arrival_address,
:departure_address, :arrival_city, :departure_city,
:reservation_cost, :total_additional_cost, :user_attributes, :user_id, :garage_id,
user_attributes: [:id, :name, :surname, :email, :phone],
garage_attributes: [:id]
)
end
===========================
Booking:
belongs_to :user
accepts_nested_attributes_for :user
===========================
##In model User:
has_many :bookings
但是,在具有相同参数的irb控制台中的@booking.user.save & @booking.save
可成功保存并且传递了true,而不会出现任何禁止的属性错误。
此Forbidden属性来自哪里?我确信我允许我在表单中发送的所有attrs,并且我认为我正确使用了accep_nested_attributes_,不是吗?
感谢您的答案。但它已经这样做了。但令人惊讶的是不起作用,这就是为什么我感到惊讶。 –
您能否请您编写您的项目的rails版本,并且您正在控制器的新动作中建立用户对象? –
请参阅此链接:http://stackoverflow.com/questions/22990103/activemodelforbiddenattributeserror-using-update-attributes-having-created-par –