HarryLucas
HarryLucas

Reputation: 159

Rails 4 acts_as_taggable_on :tag_list => [] strong param not working?

I am trying to use tagging on my notes model through this gem. However, even though I have explicitly added (2 seperate ways) :tag_list => [] to my strong params of my notes controller when ever I try and submit them, I still get an unpermitted parameter error in the logs? I have ran bundle install, and migrate as well.

Here are my files:

#/models/note.rb
class Note < ActiveRecord::Base
 belongs_to :user
  acts_as_taggable

  validates_presence_of :name, :note_text, :note_style, :note_description
end

#/controllers/notes_controller.rb
.
.
.
 def note_params
    params.require(:note).permit(:name, :note_style, :note_text, :note_description, :tag_list => [])
 end

and my notes form:

 .form-group
      = f.label :tag_list, "Tags (seperate by comma)"
      = f.text_field :tag_list, class: 'form-control'

I followed everything from the gem but I still can't get it to work.

Upvotes: 0

Views: 547

Answers (4)

cmititiuc
cmititiuc

Reputation: 680

:tag_list => [] is needed when you're using a select tag in your form because a select tag returns an array when the form is submitted.

Since you are using a text field instead of a select tag, you are not returning an array when the form is submitted but a single value (the string in the text field), so you only need :tag_list in your permit parameters list.

Upvotes: 1

sansarp
sansarp

Reputation: 1466

The acts-as-taggable-on gem uses polymorphic association. In your case param as an empty array could not be initialized which might have caused the problem. Hope this clears your confusion. :-)

Upvotes: 1

HarryLucas
HarryLucas

Reputation: 159

I actually got it to work by making my strong params by adding just :tag_list. Any idea why this works and not how they specified to do it in the gem documentation?

#/controllers/notes_controller.rb
.
.
.
 def note_params
    params.require(:note).permit(:name, :note_style, :note_text, :note_description, :tag_list, :tag_list => [])
 end

Upvotes: 2

sansarp
sansarp

Reputation: 1466

Try using this in the list of permitted params :tag_list => [:name, :taggings_count, :count]

Upvotes: 0

Related Questions