作为标记的行为
我试图在安装程序上使用我的Rails 5应用程序来获取标签。
我有一个叫做Randd :: Field.rb的模型,它有一个名为title的属性。 我想将这些标题用作提案模型的标签。
我的Proposal.rb具有:
class Proposal <ApplicationRecord include 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 <ActiveRecord :: Base acts_as_taggable#acts_as_taggable_on的别名:tags acts_as_taggable_on:skills,:interest end
class UsersController <ApplicationController def user_params params.require(:user).permit(:name,:tag_list)## Rails 4强参数用法end end
为什么不允许的参数包含:skill_list和an: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
您发送* _list参数的数组。 尝试在permit方法中为每个_list添加[]:
params.require(:proposal).permit(
:title,
randd_maturities_list: [],
randd_field_list: [],
randd_purposes_list: [],
randd_activities_list: [])
链接地址: http://www.djcxy.com/p/35961.html
上一篇: Acts as Taggable On
下一篇: Ransack and acts