小编典典

如何允许具有强参数的数组

all

我有一个正常运行的 Rails 3 应用程序,它使用 has_many :through 关联,但我将其重新制作为 Rails 4 应用程序,让我可以在
Rails 4 版本中保存关联模型的 ID。

这些是三个相关模型对于两个版本是相同的。

分类.rb

class Categorization < ActiveRecord::Base

  belongs_to :question
  belongs_to :category
end

问题.rb

has_many :categorizations
has_many :categories, through: :categorizations

类别.rb

has_many :categorizations
has_many :questions, through: :categorizations

在这两个应用程序中,类别 ID 都像这样传递到创建操作中

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

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

 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 中的参数后,我在服务器日志中收到此错误

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 中的允许参数

   def question_params

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

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

这些是两个应用程序中相同的三个表

 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 应用程序的创建操作

  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 应用程序的创建操作

   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 方法

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

阅读 105

收藏
2022-04-24

共1个答案

小编典典

https://github.com/rails/strong_parameters看起来像文档的相关部分:

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

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

params.permit(:id => [])

在我的应用程序中,category_ids 被传递给数组中的创建操作

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

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

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

现在完美运行!

重要提示: 正如@Lenart 在注释中指出的那样,数组声明必须 位于属性列表的末尾 ,否则会出现语法错误。)

2022-04-24