我正在尝试将设置上的行为设置为可用于我的Rails 5应用程序。
我有一个名为Randd :: Field.rb的模型,它有一个名为:title的属性。我想将这些标题用作我的提案模型的标签。
我的Proposal.rb有:
class Proposal< ApplicationRecord 包括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文档令人困惑,因为它显示:
设置
class User&lt; ActiveRecord :: Base acts_as_taggable #Alias for acts_as_taggable_on:tags acts_as_taggable_on:技能,:兴趣 端
类UsersController&lt; ApplicationController def user_params params.require(:user).permit(:name,:tag_list)## Rails 4强params使用结束
为什么允许的参数不包括: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" = $1 AND "taggings"."taggable_type" = $2 [["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" = $1 AND "taggings"."taggable_type" = $2 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" = $1 AND "taggings"."taggable_type" = $2 [["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" = $1 AND "taggings"."taggable_type" = $2 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
答案 0 :(得分:0)
您为* _list参数发送数组。尝试在permit方法中为每个_list添加[]:
params.require(:proposal).permit(
:title,
randd_maturities_list: [],
randd_field_list: [],
randd_purposes_list: [],
randd_activities_list: [])