我在保存simple_form.fields_for时遇到了麻烦 - 禁用属性错误
预订控制器中的'create'动作看起来如此:
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,我认为我正确地使用accepts_nested_attributes_for,不是吗?
答案 0 :(得分:0)
只需按照以下内容在控制器私有方法中定义user_attributes:
private
def user_params
params.require(
:user
).permit(
:first_name,
:last_name,
:job_title
)
end
如果您正在使用嵌套字段,只需在此属性中添加嵌套属性,如下所示:
private
def user_params
params.require(
:user
).permit(
:first_name,
:last_name,
:job_title,
addresses_attributes: [:address_1, :address_2]
)
end
通过记住模型关联来编写嵌套属性, 希望这对你有用。 :)