how to permit an array with strong parameters
Asked Answered
T

6

306

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
Tabloid answered 14/5, 2013 at 17:33 Comment(2)
What does the create action look like in both apps?Furthermost
@Furthermost I added the two create actions. ThanksTabloid
T
607

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

Tabloid answered 15/5, 2013 at 2:48 Comment(11)
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 =>Zwickau
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.Toleration
How can I do this as a separate param for category_idsAdulteress
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/11502Girardo
This only worked for me when I put it at the end of the list of approved params.Kalvn
@Zwickau Thanks, I thought I was going crazy. This should at least appear in the strong_params READMEGaw
In case someone needs to have array of string in the strong params all you have to do is include postgres_ext and the add in the list of params to permit :some_array_column => []Bosnia
Hehe, I also wondered why it is not allowing my Array column which has already set in permit. I found the solution going through the rails code. Its restricts through defining 'scalar types'Stulin
@Zwickau you can add it as hash if you want to avoid syntax error. params.permit(:foo, { bar: [] }, :zoo).Wideawake
This answer should be updated with sameer's comment. The array doesn't have to be last, it just needs to be passed inside curly braces so Ruby knows it's a single argument - github.com/rails/strong_parameters#nested-parametersMaighdiln
What an utterly terrible design. Why on earth do the hash values need to be sorted by value type. "magic"Jotun
M
119

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.
Monto answered 17/5, 2015 at 9:53 Comment(4)
and arrays of arrays are not supported (yet): github.com/rails/rails/issues/23640String
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)Ageless
You are a Champ!Muttra
This answer should be edited with mastBlasta's comment (I tried but the suggested edit queue is full)Maighdiln
M
30

The array should be the last argument of the permit method.

params.permit(:id => [])

Also since Ruby 1.9 or newer, you can use:

params.permit(id: [])
Maggard answered 6/7, 2017 at 14:47 Comment(4)
please not that the array should be the last argument of the permit methodMerrymerryandrew
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)Dustidustie
That's not correct @MatiasElorriaga... see mastaBlasta's comment on Brian's answer, or github.com/rails/strong_parameters#nested-parametersMaighdiln
this saved me in a situation like this , thanks a lot. game_results: [:score, :date ,fouls: [],players: [],subs: []]Dactylogram
I
23

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]])
Iridaceous answered 3/5, 2017 at 15:49 Comment(1)
took me way to long to find thisSepulture
A
11

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)

Alonzo answered 17/2, 2021 at 8:19 Comment(0)
S
9

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: []]])
Skelton answered 12/10, 2018 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.