ChatGPT解决这个技术问题 Extra ChatGPT

how to permit an array with strong parameters

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
What does the create action look like in both apps?
@bennick I added the two create actions. Thanks

Y
Yarin

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.)


I've also noticed that declarations for arrays should be at the end of the attributes list. Otherwise I get a syntax error syntax error, unexpected ')', expecting =>
The reason array declarations (nested params) are at the end is that ActionController::Parameters.permit expects each argument to be a Hash or a Symbol. Ruby magic will turn all key value pairs s at the end of a method call into one hash, but Ruby won't know what to do if you mix symbols with key/value pairs in your method call.
if category_ids is not an array (e.g. a string was sent) then rails totally goes nuts and raises a ERROR TypeError: expected Array (got String) for param `category_ids' Is that a bug in rails? Edit: yes it is: github.com/rails/rails/issues/11502
@Lenart Thanks, I thought I was going crazy. This should at least appear in the strong_params README
@Lenart you can add it as hash if you want to avoid syntax error. params.permit(:foo, { bar: [] }, :zoo).
B
Brian

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:

array should be the last argument of the permit method. 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.


and arrays of arrays are not supported (yet): github.com/rails/rails/issues/23640
array does not need to be at the end, but you need to make sure you have valid Ruby. params.permit(:foo, array: [:key1, :key2], :bar) would not be valid ruby as the interpreter expects hash key:value pairs after the first set. To have valid Ruby you need params.permit(:foo, {array: [:key1, :key2]}, :bar)
You are a Champ!
This answer should be edited with mastBlasta's comment (I tried but the suggested edit queue is full)
M
Matias Seguel

It should be like

params.permit(:id => [])

Also since rails version 4+ you can use:

params.permit(id: [])

please not that the array should be the last argument of the permit method
That hash syntax that you said apply for Rails v4+ is actually a syntax available in Ruby 1.9 & newer, not the Rails framework (although Rails 4 requires Ruby 1.9.3 or newer)
That's not correct @MatiasElorriaga... see mastaBlasta's comment on Brian's answer, or github.com/rails/strong_parameters#nested-parameters
F
Fellow Stranger

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]])

took me way to long to find this
D
Daniel Duque

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: []]])

D
Dev Bhatt

when you want to permit multiple array fields you will have to list array fields at last while permitting ,as given -

params.require(:questions).permit(:question, :user_id, answers: [], selected_answer: [] )

(this works)