Search code examples
ruby-on-railsruby-on-rails-4acts-as-taggable-on

Can't save tags using ActsAsTaggableOn


Via console I can save tags but via a form in View I can not save tags.

In the model:

class Person < ActiveRecord::Base
    has_many :user
    has_paper_trail
    acts_as_taggable

    @tags = Person.tag_counts_on(:tags)

    def admin_permalink
    admin_post_path(self)
  end
end

I've read that I should include attr_accessible :tag_list in the Model, but doing so causes an error in Rails 4. My form partial has

  <%= f.label :tag_list, "Tags (separated by commas)" %><br />
  <%= f.text_field :tag_list %>

Any help would be much appreciated, thanks


Solution

  • Instead of 'tag_counts_on(:tags)' Try 'acts_as_taggable_on :tags'


    [Edited]
    I solved it by fixing lots of deprecated syntax and incorrect use of your ruby code.

    1. First of all, the cause of that error was because you didn't properly whitelist :tag_list attribute.

    def person_params
      params.require(:person).permit(:name, :twitter, :facebook, :instagram, :vine, :tag_list => [])
    end
    


    def person_params
      params.require(:person).permit(:name, :twitter, :facebook, :instagram, :vine, :tag_list)
    end
    

    2. I think you followed some kind of tutorials right?
    I saw the similar code from one of tut articles just right, and it was written on 2012.
    Following tuts is good practice, but always check the date.

    3. I suggest you to upgrade Rails to v4.1.0 at least
    because your Active Admin gem complains the support for below Rails v4.1.0 will be dropped soon.

    4. I found the label had incorrect attribute, which is :tags, so I changed it to :tag_list.

    <div class="field">
    <%= f.label :tag_list, "Tags (separated by commas)" %><br />
    <%= f.text_field :tag_list %>
    </div>
    


    preview

    You may clone this repo from: https://github.com/seoyoochan/adriangrantdotorg-urbanbook