Zippo9
Zippo9

Reputation: 706

Acts As Taggable On gem rails not working

I cannot get the Act_as_taggable gem to work. I have followed several tutorials and even viewed the out of date Railscast on this issue but my main problem in with the method tag_list, I am using Rails 4

I have done the following:

gem 'acts-as-taggable-on', '~> 3.4' (bundled) rake acts_as_taggable_on_engine:install:migrations (rake db:migrate; restart server) Added acts_as_taggable to my Blog.rb Passed tag_list through safe params in BlogsController Placed this into my _form partial:

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

When I go to edit a Blog post the tags are not separated by commas even though when I submitted the form I placed commas after each tag. In rails console I have verified that tag_list is not a method because I keep getting the error

      undefined method `tag_list' for #<Class:0x007fa668cb03e8>

And therefore I haven't been able to add Tags to any Blog post yet. Please help

Upvotes: 2

Views: 780

Answers (2)

carlosescri
carlosescri

Reputation: 309

EDIT: I'm using Rails 4 but and the gem version is 3.5.0.


I'm having some issues with the same gem and this is what I've done.

class Common < ActiveRecord::Base
  # ...
  acts_as_taggable
  # ...
end

In the controller:

class CommonsController < ApplicationController
  # ...
  def my_params
    params.require(:common).permit(
      # ...
      :tag_list,
      # ...
    )
  end
  #...
end

And in the template:

<%= form_for @common do |f| %>
  <!-- ... -->
  <p>
    <%= f.label :tag_list %> <%= f.text_field :tag_list %>
  </p>
  <!-- ... -->
<% end %>

This worked for me, but:

  • When I write a list of tags separated by comma, they are saved correctly. For example: "red, green, blue".
  • But, when the form is loaded again, the commas disappeared from the text field, like in "red green blue". If I save the form again then a new tag is created: "red green blue".

If I create a virtual attribute in the model it works, but I think that breaks the normal behavior of the tag_list method in the gem:

class Common < ActiveRecord::Base
  # ...
  acts_as_taggable
  # ...
  def tag_list
    tags.join(', ')
  end
end

Hope this helps you in any way...

Upvotes: 2

jayesh
jayesh

Reputation: 2492

Please make sure you add this lines on Model

acts_as_taggable # Alias for acts_as_taggable_on :tags
  acts_as_taggable_on :skills

use your model object in form

@blog.tag_list_on(:skills_tags)

Upvotes: 0

Related Questions