how to permit an array with strong parameters

纵然是瞬间 提交于 2019-11-26 00:17:35

问题


I have a functioning Rails 3 app that uses has_many :through associations which is not, as I remake it as a Rails 4 app, letting me save ids from the associated model in the Rails 4 version.

These are the three relevant models are the same for the two versions.

Categorization.rb

class Categorization < ActiveRecord::Base

  belongs_to :question
  belongs_to :category
end

Question.rb

has_many :categorizations
has_many :categories, through: :categorizations

Category.rb

has_many :categorizations
has_many :questions, through: :categorizations

In both apps, the category ids are getting passed into the create action like this

  \"question\"=>{\"question_content\"=>\"How do you spell car?\", \"question_details\"=>\"blah \", \"category_ids\"=>[\"\", \"2\"],

In the Rails 3 app, when I create a new question, it inserts into questions table and then into the categorizations table

 SQL (82.1ms)  INSERT INTO \"questions\" (\"accepted_answer_id\", \"city\", \"created_at\", \"details\", \"province\", \"province_id\", \"question\", \"updated_at\", \"user_id\") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [[\"accepted_answer_id\", nil], [\"city\", \"dd\"], [\"created_at\", Tue, 14 May 2013 17:10:25 UTC +00:00], [\"details\", \"greyound?\"], [\"province\", nil], [\"province_id\", 2], [\"question\", \"Whos\' the biggest dog in the world\"], [\"updated_at\", Tue, 14 May 2013 17:10:25 UTC +00:00], [\"user_id\", 53]]
  SQL (0.4ms)  INSERT INTO \"categorizations\" (\"category_id\", \"created_at\", \"question_id\", \"updated_at\") VALUES (?, ?, ?, ?)  [[\"category_id\", 2], [\"created_at\", Tue, 14 May 2013 17:10:25 UTC +00:00], [\"question_id\", 66], [\"updated_at\", Tue, 14 May 2013 17:10:25 UTC +00:00]]

In the rails 4 app, after it processes the parameters in QuestionController#create, I\'m getting this error in the server logs

Unpermitted parameters: category_ids

and the question is only getting inserted into the questions table

 (0.2ms)  BEGIN
  SQL (67.6ms)  INSERT INTO \"questions\" (\"city\", \"created_at\", \"province_id\", \"question_content\", \"question_details\", \"updated_at\", \"user_id\") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING \"id\"  [[\"city\", \"dd\"], [\"created_at\", Tue, 14 May 2013 17:17:53 UTC +00:00], [\"province_id\", 3], [\"question_content\", \"How\'s your car?\"], [\"question_details\", \"is it runnign\"], [\"updated_at\", Tue, 14 May 2013 17:17:53 UTC +00:00], [\"user_id\", 12]]
   (31.9ms)  COMMIT

Although I am not storing the category_ids on the Questions model, I set category_ids as a permitted parameter in the questions_controller

   def question_params

      params.require(:question).permit(:question_details, :question_content, :user_id, :accepted_answer_id, :province_id, :city, :category_ids)
    end

Can anyone explain how I\'m supposed to save the category_ids? Note, there is no create action in the categories_controller.rb of either app.

These are the three tables that are the same in both apps

 create_table \"questions\", force: true do |t|
    t.text     \"question_details\"
    t.string   \"question_content\"
    t.integer  \"user_id\"
    t.integer  \"accepted_answer_id\"
    t.datetime \"created_at\"
    t.datetime \"updated_at\"
    t.integer  \"province_id\"
    t.string   \"city\"
  end

 create_table \"categories\", force: true do |t|
    t.string   \"name\"
    t.datetime \"created_at\"
    t.datetime \"updated_at\"
  end

  create_table \"categorizations\", force: true do |t|
    t.integer  \"category_id\"
    t.integer  \"question_id\"
    t.datetime \"created_at\"
    t.datetime \"updated_at\"
  end

Update

This is the create action from the Rails 3 app

  def create
      @question = Question.new(params[:question])
      respond_to do |format|
      if @question.save
        format.html { redirect_to @question, notice: \'Question was successfully created.\' }
        format.json { render json: @question, status: :created, location: @question }
      else
        format.html { render action: \"new\" }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
end

This is the create action from the Rails 4 app

   def create
      @question = Question.new(question_params)

       respond_to do |format|
      if @question.save
        format.html { redirect_to @question, notice: \'Question was successfully created.\' }
        format.json { render json: @question, status: :created, location: @question }
      else
        format.html { render action: \"new\" }
        format.json { render json: @question.errors, status: :unprocessable_entity }
      end
    end
    end

This is the question_params method

 private
    def question_params 
      params.require(:question).permit(:question_details, :question_content, :user_id, :accepted_answer_id, :province_id, :city, :category_ids)
    end

回答1:


This https://github.com/rails/strong_parameters seems like the relevant section of the docs:

The permitted scalar types are String, Symbol, NilClass, Numeric, TrueClass, FalseClass, Date, Time, DateTime, StringIO, IO, ActionDispatch::Http::UploadedFile and Rack::Test::UploadedFile.

To declare that the value in params must be an array of permitted scalar values map the key to an empty array:

params.permit(:id => [])

In my app, the category_ids are passed to the create action in an array

"category_ids"=>["", "2"],

Therefore, when declaring strong parameters, I explicitly set category_ids to be an array

params.require(:question).permit(:question_details, :question_content, :user_id, :accepted_answer_id, :province_id, :city, :category_ids => [])

Works perfectly now!

(IMPORTANT: As @Lenart notes in the comments, the array declarations must be at the end of the attributes list, otherwise you'll get a syntax error.)




回答2:


If you want to permit an array of hashes(or an array of objects from the perspective of JSON)

params.permit(:foo, array: [:key1, :key2])

2 points to notice here:

  1. array should be the last argument of the permit method.
  2. you should specify keys of the hash in the array, otherwise you will get an error Unpermitted parameter: array, which is very difficult to debug in this case.



回答3:


It should be like

params.permit(:id => [])

Also since rails version 4+ you can use:

params.permit(id: [])



回答4:


If you have a hash structure like this:

Parameters: {"link"=>{"title"=>"Something", "time_span"=>[{"start"=>"2017-05-06T16:00:00.000Z", "end"=>"2017-05-06T17:00:00.000Z"}]}}

Then this is how I got it to work:

params.require(:link).permit(:title, time_span: [[:start, :end]])



回答5:


I can't comment yet but following on Fellow Stranger solution you can also keep nesting in case you have keys which values are an array. Like this:

filters: [{ name: 'test name', values: ['test value 1', 'test value 2'] }]

This works:

params.require(:model).permit(filters: [[:name, values: []]])


来源:https://stackoverflow.com/questions/16549382/how-to-permit-an-array-with-strong-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!