Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to permit an array with strong parameters
    text
    copied!<p>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. </p> <p>These are the three relevant models are the same for the two versions. </p> <p>Categorization.rb</p> <pre><code>class Categorization &lt; ActiveRecord::Base belongs_to :question belongs_to :category end </code></pre> <p>Question.rb</p> <pre><code>has_many :categorizations has_many :categories, through: :categorizations </code></pre> <p>Category.rb</p> <pre><code>has_many :categorizations has_many :questions, through: :categorizations </code></pre> <p>In both apps, the category ids are getting passed into the create action like this</p> <pre><code> "question"=&gt;{"question_content"=&gt;"How do you spell car?", "question_details"=&gt;"blah ", "category_ids"=&gt;["", "2"], </code></pre> <p>In the Rails 3 app, when I create a new question, it inserts into questions table and then into the categorizations table </p> <pre><code> 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]] </code></pre> <p>In the rails 4 app, after it processes the parameters in QuestionController#create, I'm getting this error in the server logs</p> <pre><code>Unpermitted parameters: category_ids </code></pre> <p>and the question is only getting inserted into the questions table</p> <pre><code> (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 </code></pre> <p>Although I am not storing the category_ids on the Questions model, I set category_ids as a permitted parameter in the questions_controller</p> <pre><code> def question_params params.require(:question).permit(:question_details, :question_content, :user_id, :accepted_answer_id, :province_id, :city, :category_ids) end </code></pre> <p>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.</p> <p>These are the three tables that are the same in both apps</p> <pre><code> 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 </code></pre> <p>Update</p> <p>This is the create action from the Rails 3 app</p> <pre><code> 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 </code></pre> <p>This is the create action from the Rails 4 app</p> <pre><code> 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 </code></pre> <p>This is the question_params method</p> <pre><code> private def question_params params.require(:question).permit(:question_details, :question_content, :user_id, :accepted_answer_id, :province_id, :city, :category_ids) end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload