【问题标题】:Unpermitted parameters nested attributes - rails不允许的参数嵌套属性 - rails
【发布时间】:2017-04-26 23:45:21
【问题描述】:

我正在尝试向 2 个表格提交一个表单,但不知何故,我在滑坡参数中的 joins: ['sources'], :landslide_id 和 found unpermitted parameter: sources 这一行收到了这个语法错误 unexpected '\n'。这是所有文件

模型

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"=>""}

【问题讨论】:

  • 从请求中添加整个日志,包括参数哈希和整个表单。不清楚你在做什么。
  • PS 不要这样做%label.col-sm-2.col-form-label{for: "textinput"}Text。请改用 Rails 助手 f.label - 您设置了错误的 for 属性 - 它应该与输入中的 name 属性相同。 guides.rubyonrails.org/form_helpers.html
  • 请求日志和截图更新
  • 真的是这样吗? = f.fields_for :sources 不可能给出该参数哈希值。请包含完整的代码。
  • 好吧,我解决了这个问题。现在我遇到了另一个错误here。

标签: ruby-on-rails nested-attributes


【解决方案1】:

发现未经许可的参数:来源

根据您的表单,来源似乎位于名为 sources 而不是 sources_attributes 的参数中。编辑您的landslide_params,将sources_attributes 更改为来源。

请问set_landslide 试图渲染什么,或者如果我在下面错了,请纠正我?将joins 放在新行上会导致错误。我认为您正在尝试执行以下操作:

landslide = Landslide.find(params[:total_id])
render json: landslide.to_json(:include => { :sources => { landslide_params[:sources] }})

这会给你一个带有山体滑坡对象和源数组的 json。滑坡 id 应该在滑坡对象内。这当然假设这就是你想要的。

【讨论】:

  • 如果表单实际上与问题中发布的一样,它应该提供嵌套在sources_attributes 中的参数。这就是fields_for 所做的。我最初在另一个问题中指出了 OP 的正确方向,但他似乎只是随意地复制粘贴东西。
  • 似乎有一个源参数,所以这张照片肯定缺少一些东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
  • 1970-01-01
相关资源
最近更新 更多