Search code examples
ruby-on-railsrubyacts-as-taggable-onwhite-labelling

Rails 5 - Acts as Taggable On - unpermitted params for specific tag list


I am trying to get acts as taggable on setup to work with my Rails 5 app.

I have a model called Randd::Field.rb, which has an attribute called :title in it. I want to use those titles as tags for my proposal model.

My Proposal.rb has:

class Proposal < ApplicationRecord include Statesman::Adapters::ActiveRecordQueries

  acts_as_taggable
  acts_as_taggable_on :randd_maturities, :randd_fields, :randd_purposes, :randd_activities

My proposal form.html.erb has:

<%= f.collection_select :randd_field_list, Randd::Field.order(:title), :id, :title, {}, {multiple: true} %>

My proposal controller whitelists the randd_field_list with:

  def proposal_params
      params.require(:proposal).permit(:title, :randd_maturities_list, :randd_field_list, :randd_purposes_list, :randd_activities_list)

When I save all of this and try to add a tag to a proposal, I get an error. The server log shows:

Unpermitted parameter: randd_field_list

This doesn't make any sense because its a problem that comes up when the attribute is not whitelisted in the permitted params - which I have done.

Can anyone see what needs to be done in order to save tags via the proposal form?

I find the gem documentation confusing because it shows:

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

Why doesn't the permitted params include a :skill_list and an :interest_list given that those specific models are used as tags? Am I supposed to add "tag_list" to my proposal permitted params even when the only tags that can be added are from the titles defined in the specific models I've listed in:

acts_as_taggable_on :randd_maturities, :randd_fields, :randd_purposes, :randd_activities

The server log shows:

  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

Solution

  • You send an array for *_list parameters. Try to add [] for every _list in permit method:

    params.require(:proposal).permit(
      :title, 
      randd_maturities_list: [], 
      randd_field_list: [], 
      randd_purposes_list: [], 
      randd_activities_list: [])