如何允许具有强参数的数组 [英] how to permit an array with strong parameters

查看:61
本文介绍了如何允许具有强参数的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行正常的Rails 3应用程序,该应用程序使用has_many:through关联,但没有,因为我将其重新创建为Rails 4应用程序,让我从Rails 4版本中的关联模型中保存了ID.

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

Categorization.rb

class Categorization < ActiveRecord::Base

  belongs_to :question
  belongs_to :category
end

Question.rb

Question.rb

has_many :categorizations
has_many :categories, through: :categorizations

Category.rb

Category.rb

has_many :categorizations
has_many :questions, through: :categorizations

在这两个应用程序中,类别ID都被传递到像这样的create动作中

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"],

在Rails 3应用程序中,当我创建一个新问题时,它会插入问题表,然后再插入分类表

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

在Rails 4应用程序中,在处理QuestionController#create中的参数之后,我在服务器日志中收到此错误

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

问题只插入到问题表中

 (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

尽管我没有将category_ids存储在Questions模型中,但是我将category_ids设置为questions_controller中的允许参数

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

谁能解释我应该如何保存category_ids?请注意,两个应用程序的category_controller.rb中都没有创建操作.

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

更新

这是Rails 3应用程序中的创建动作

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

这是Rails 4应用程序中的创建动作

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

这是question_params方法

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

推荐答案

https://github.com/rails/strong_parameters 似乎是文档的相关部分:

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

允许的标量类型为String,Symbol,NilClass,Numeric,TrueClass,FalseClass,Date,Time,DateTime,StringIO,IO, ActionDispatch :: Http :: UploadedFile和Rack :: Test :: UploadedFile.

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

要声明params中的值必须是允许的标量值数组,请将键映射到空数组:

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

在我的应用中,category_ids以数组的形式传递给create操作

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

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

因此,在声明强参数时,我明确地将category_ids设置为数组

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

现在完美运行!

(重要提示::如注释中的@Lenart所述,数组声明必须位于属性列表的末尾 ,否则会出现语法错误. )

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

这篇关于如何允许具有强参数的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆