问题描述
我正在尝试向2个表提交表单,但不知何故我在滑坡参数的joins: ['sources'], :landslide_id
和found unpermitted parameter: sources
行遇到了此语法错误unexpected '\n'
.这是所有文件
I'm trying to submit a form to 2 tables but somehow I got this syntax error unexpected '\n'
at this line joins: ['sources'], :landslide_id
and found unpermitted parameter: sources
in landslide params. Here's all the files
模型
class Landslide < ApplicationRecord
has_many :sources, dependent: :destroy
accepts_nested_attributes_for :sources
class Source < ApplicationRecord
belongs_to :landslide
end
landslides_controller.rb
def new
@landslide = Landslide.new
@landslide.sources.build
end
# POST /landslides
def create
@landslide = Landslide.new(landslide_params)
@landslide.save
end
private
# Use callbacks to share common setup or constraints between actions.
def set_landslide
render json: Landslide.find(params[:total_id]),
joins: ['sources'], :landslide_id
end
# Only allow a trusted parameter "white list" through.
def landslide_params
params.require(:landslide).permit(:start_date, :continent, :country, :location, :landslide_type, :lat, :lng, :mapped, :trigger, :spatial_area, :fatalities, :injuries, :notes, sources_attributes: [ :url, :text ])
end
sources_controller.rb
def set_source
@source = Source.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def source_params
params.require(:source).permit(:url, :text)
end
_form.html.haml
= form_for :landslide, :url => {:controller => 'landslides', :action => 'create'} do |f|
#something
%fieldset
%legend Source
= f.fields_for :sources do |s|
.form-group.row
%label.col-sm-2.col-form-label{for: "textinput"}URL
.col-sm-10
= s.text_field :url, class: "form-control"
.form-group.row
%label.col-sm-2.col-form-label{for: "textinput"}Text
.col-sm-10
= s.text_field :text, class: "form-control"
请求
{"utf8"=>"✓",
"authenticity_token"=>"W3m2dLTGyuPCbP6+pStWDfgpIbPzGdl4tvf01vMAbyozzkimqlXH4B/RtwBcsLb+iiBqms7EHagY+Anbpo4zNg==",
"landslide"=>
{"start_date(3i)"=>"27",
"start_date(2i)"=>"4",
"start_date(1i)"=>"2017",
"continent"=>"Africa",
"country"=>"Country",
"location"=>"Location",
"landslide_type"=>"1",
"lat"=>"1",
"lng"=>"1",
"mapped"=>"False",
"spatial_area"=>"1",
"fatalities"=>"1",
"injuries"=>"1",
"notes"=>"1",
"trigger"=>"1",
"sources"=>{"url"=>"url", "text"=>"text"}},
"button"=>""}
推荐答案
找到了不允许的参数:来源
根据您的表单,看来源位于称为source而不是sources_attributes的参数内.编辑landslide_params
,将sources_attributes
更改为源.
Based on your form, it looks like sources are inside a param called sources rather than sources_attributes. Edit your landslide_params
, changing sources_attributes
to sources.
我可以问一下set_landslide
试图渲染什么,或者如果下面的我写错了,请更正我的意思吗?将joins
放在新行上会导致错误.我认为您正在尝试执行以下操作:
May I ask what set_landslide
is trying to render, or correct me if I am wrong below? Placing joins
on a new line causes the error. I am thinking you're trying to do something like:
landslide = Landslide.find(params[:total_id])
render json: landslide.to_json(:include => { :sources => { landslide_params[:sources] }})
这会给你一个带有滑坡对象和一个sources数组的json.滑坡id应在滑坡对象内.当然,这是假设您要这样做的.
Which would give you a json with the landslide object and a sources array. The landslide id should be within the landslide object. This of course assumes that's what you were going for.
这篇关于不允许的参数嵌套属性-Rails的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!