Search code examples
ruby-on-railstaggingnomethoderror

Act As Taggable On --Adding Tags to a Model


I want to add a bunch of predefined tags to a model.

I have added it to my params in the Controller:

def photo_params
  params.require(:photo).permit(:image,:title, :description, :styles_list)
end

I have also included:

act_as_taggable_on :styles

Now I am just trying to add the tags but in my console I keep getting the following error: Tried Photo.styles_list.add

got

NoMethodError: undefined method `styles_list' for #<Class:0x007fa9ee74aac8>

then I tried styles_list.add as described in documentation https://github.com/mbleigh/acts-as-taggable-on/wiki/Add-Tags

but I still get the same error, what am I doing wrong?


Solution

  • Photo.styles_list.add is trying to invoke the styles_list method on the Photo class. That is incorrect. The styles_list method is available on a photo object.

    Create/find a photo object as follows:

    @photo = Photo.new
    
    @photo = Photo.find(params[:id])
    

    Then a style can be tagged with the following:

    @photo.styles_list.add("awesome")
    

    See https://github.com/mbleigh/acts-as-taggable-on#usage for getting a better understanding of how the gem can be used.