2017-04-26 97 views
1

我試圖提交一個表格到2個表格,但不知何故我得到了這個語法錯誤unexpected '\n'在這條線joins: ['sources'], :landslide_idfound unpermitted parameter: sources in landslide params。這裏的所有文件未經許可的參數嵌套屬性 - 導軌

模式

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

Error screenshot

+0

從請求中添加整個日誌,包括參數散列和整個表單。它不清楚你在哪裏做什麼。 – max

+0

PS不會這樣做'%label.col-sm-2.col-form-label {for:「textinput」} Text'。使用Rails helper'f.label'代替 - 設置錯誤的'for'屬性 - 它應該與輸入中的'name'屬性相同。 http://guides.rubyonrails.org/form_helpers.html – max

+0

請求日誌和截圖更新 –

回答

0

發現未經許可的參數:來源

基於表單上,它看起來LIK e來源在一個名爲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的滑坡對象和一個sources數組。滑坡ID應位於滑坡對象內。這當然假定這就是你要做的。

+0

如果表單實際上是在問題中發佈的,它應該給出嵌套在'sources_attributes'中的參數。那就是'fields_for'的作用。我最初指出OP在另一個問題中是朝着正確的方向發展的,但他似乎只是複製粘貼東西而已。 – max

+1

似乎有一個來源參數雖然,所以肯定是從這張照片中缺少的東西。 – CareBearKate