我有一个form
将数组传递给参数中的控制器,并且它会导致ActiveModel::ForbiddenAttributesError
。我的create
函数调用另一个函数来修复这些数组参数并将这些部分组合成一个字符串,但我认为那时已经太晚了。
PARAMS
{ "remote"=>
{ "name"=>"",
"start_date"=>"9/27/2016",
"email"=>"",
"allergies"=>["Peanuts", "Soy Protein", "Dairy", ""],
}
}
控制器
def create
new_params = fix_params(params, ["allergies"])
remote_params = new_params["remote"]
@remote = Remote.new(remote_params)
respond_to do |format|
if @remote.save
format.html { redirect_to root_path, notice: 'Remote was successfully created.' }
format.json { render :show, status: :created, location: @remote }
else
format.html { render :new }
format.json { render json: @remote.errors, status: :unprocessable_entity }
end
end
end
def fix_params(params, fields)
fields.each do |field|
to_change = params[:remote][field]
new_param = to_change.join(", ").strip()
params[:remote][field] = new_param || ""
end
return params
end
也许有更好的方法来传递这些变量?
答案 0 :(得分:0)
错误发生的原因是unpermitted parameters
因为我从未明确表示将会在array
中传递params.require(:remote).permit(:name, :start_date, :email, :allergies => [])
。
我修改了我的强参数以包含# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /