Rails 5 - 作为 Taggable On - 特定标签列表的不允许参数
Rails 5 - Acts as Taggable On - unpermitted params for specific tag list
我正在尝试让行为在设置时可标记以与我的 Rails 5 应用程序一起使用。
我有一个名为 Randd::Field.rb 的模型,其中有一个名为 :title 的属性。我想将这些标题用作我的提案模型的标签。
我的 Proposal.rb 有:
class 提案 < 申请记录
包括 Statesman::Adapters::ActiveRecordQueries
acts_as_taggable
acts_as_taggable_on :randd_maturities, :randd_fields, :randd_purposes, :randd_activities
我的提案 form.html.erb 有:
<%= f.collection_select :randd_field_list, Randd::Field.order(:title), :id, :title, {}, {multiple: true} %>
我的提案控制器将 randd_field_list 列入白名单:
def proposal_params
params.require(:proposal).permit(:title, :randd_maturities_list, :randd_field_list, :randd_purposes_list, :randd_activities_list)
当我保存所有这些并尝试向提案添加标签时,出现错误。服务器日志显示:
Unpermitted parameter: randd_field_list
这没有任何意义,因为当属性未在允许的参数中列入白名单时会出现问题 - 我已经做到了。
任何人都可以看到需要做什么才能通过提案表格保存标签吗?
我发现 gem 文档令人困惑,因为它显示:
Setup
class User < ActiveRecord::Base acts_as_taggable # Alias for
acts_as_taggable_on :tags acts_as_taggable_on :skills, :interests
end
class UsersController < ApplicationController def user_params
params.require(:user).permit(:name, :tag_list) ## Rails 4 strong params usage end end
为什么允许的参数不包含 :skill_list 和 :interest_list,因为这些特定模型用作标签?我是否应该将 "tag_list" 添加到我的提案允许的参数中,即使可以添加的唯一标签来自我列出的特定模型中定义的标题:
acts_as_taggable_on :randd_maturities, :randd_fields, :randd_purposes, :randd_activities
服务器日志显示:
ActsAsTaggableOn::Tagging Load (1.6ms) SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = AND "taggings"."taggable_type" = [["taggable_id", 17], ["taggable_type", "Proposal"]]
ActsAsTaggableOn::Tag Load (1.7ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = AND "taggings"."taggable_type" = AND (taggings.context = 'randd_fields' AND taggings.tagger_id IS NULL) [["taggable_id", 17], ["taggable_type", "Proposal"]]
ActsAsTaggableOn::Tagging Load (2.8ms) SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = AND "taggings"."taggable_type" = [["taggable_id", 17], ["taggable_type", "Proposal"]]
ActsAsTaggableOn::Tag Load (3.0ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = AND "taggings"."taggable_type" = AND (taggings.context = 'randd_fields' AND taggings.tagger_id IS NULL) [["taggable_id", 17], ["taggable_type", "Proposal"]]
Randd::Field Load (1.6ms) SELECT "randd_fields".* FROM "randd_fields" ORDER BY "randd_fields"."title" ASC
Processing by ProposalsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"BF7l9/0QTVN3A==", "proposal"=>{"title"=>"asdf", "byline"=>"asdf", , "randd_field_list"=>["", "1"],
Unpermitted parameter: randd_field_list
您为 *_list 参数发送了一个数组。尝试在 permit 方法中为每个 _list 添加 []:
params.require(:proposal).permit(
:title,
randd_maturities_list: [],
randd_field_list: [],
randd_purposes_list: [],
randd_activities_list: [])
我正在尝试让行为在设置时可标记以与我的 Rails 5 应用程序一起使用。
我有一个名为 Randd::Field.rb 的模型,其中有一个名为 :title 的属性。我想将这些标题用作我的提案模型的标签。
我的 Proposal.rb 有:
class 提案 < 申请记录 包括 Statesman::Adapters::ActiveRecordQueries
acts_as_taggable
acts_as_taggable_on :randd_maturities, :randd_fields, :randd_purposes, :randd_activities
我的提案 form.html.erb 有:
<%= f.collection_select :randd_field_list, Randd::Field.order(:title), :id, :title, {}, {multiple: true} %>
我的提案控制器将 randd_field_list 列入白名单:
def proposal_params
params.require(:proposal).permit(:title, :randd_maturities_list, :randd_field_list, :randd_purposes_list, :randd_activities_list)
当我保存所有这些并尝试向提案添加标签时,出现错误。服务器日志显示:
Unpermitted parameter: randd_field_list
这没有任何意义,因为当属性未在允许的参数中列入白名单时会出现问题 - 我已经做到了。
任何人都可以看到需要做什么才能通过提案表格保存标签吗?
我发现 gem 文档令人困惑,因为它显示:
Setup
class User < ActiveRecord::Base acts_as_taggable # Alias for acts_as_taggable_on :tags acts_as_taggable_on :skills, :interests end
class UsersController < ApplicationController def user_params params.require(:user).permit(:name, :tag_list) ## Rails 4 strong params usage end end
为什么允许的参数不包含 :skill_list 和 :interest_list,因为这些特定模型用作标签?我是否应该将 "tag_list" 添加到我的提案允许的参数中,即使可以添加的唯一标签来自我列出的特定模型中定义的标题:
acts_as_taggable_on :randd_maturities, :randd_fields, :randd_purposes, :randd_activities
服务器日志显示:
ActsAsTaggableOn::Tagging Load (1.6ms) SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = AND "taggings"."taggable_type" = [["taggable_id", 17], ["taggable_type", "Proposal"]]
ActsAsTaggableOn::Tag Load (1.7ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = AND "taggings"."taggable_type" = AND (taggings.context = 'randd_fields' AND taggings.tagger_id IS NULL) [["taggable_id", 17], ["taggable_type", "Proposal"]]
ActsAsTaggableOn::Tagging Load (2.8ms) SELECT "taggings".* FROM "taggings" WHERE "taggings"."taggable_id" = AND "taggings"."taggable_type" = [["taggable_id", 17], ["taggable_type", "Proposal"]]
ActsAsTaggableOn::Tag Load (3.0ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = AND "taggings"."taggable_type" = AND (taggings.context = 'randd_fields' AND taggings.tagger_id IS NULL) [["taggable_id", 17], ["taggable_type", "Proposal"]]
Randd::Field Load (1.6ms) SELECT "randd_fields".* FROM "randd_fields" ORDER BY "randd_fields"."title" ASC
Processing by ProposalsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"BF7l9/0QTVN3A==", "proposal"=>{"title"=>"asdf", "byline"=>"asdf", , "randd_field_list"=>["", "1"],
Unpermitted parameter: randd_field_list
您为 *_list 参数发送了一个数组。尝试在 permit 方法中为每个 _list 添加 []:
params.require(:proposal).permit(
:title,
randd_maturities_list: [],
randd_field_list: [],
randd_purposes_list: [],
randd_activities_list: [])