3

How can I use acts_as_taggable_on gem with strong parameters in rails 4?

Have:

class User < ActiveRecord::Base
  acts_as_tagger
end

class Post < ActiveRecord::Base
  acts_as_taggable
end

@post = current_user.tag(@post, :with => :tag_list)

def post_params
  params.require(:post).permit(:text, :user_id, :tag_list)
end

How can I add (post_params) to @post?

  • 1
    I have maintained acts_as_taggable_on for a little while. I don't think I would use it in a Rails 4 app, but build the functionality myself. – Joost Baaij Oct 11 '13 at 08:14
  • @JoostBaaij Could you recommend an alternative? – rpearce Dec 09 '13 at 22:26
  • In most cases I think the appropriate solution is to build it yourself. It's not that hard. A 3.0.0.rc1 version of acts_as_taggable_on is being prepared, as someone has taken over development of the gem. – Joost Baaij Dec 13 '13 at 09:20

2 Answers2

10

You should specify your permitted params like this:

def post_params
  params.require(:post).permit(:text, :user_id, { tag_list: [] })
end

This should make them save the tag_list in your controller action.

Holger Frohloff
  • 1,695
  • 18
  • 29
0

The tag_list is passed as an array when you submit the form. So, you should whitelist tag_list: [] in your strong params definition.

The working code is this:

def post_params
  params.
    require(:post).
    permit(:text, :user_id, tag_list: [])
end
K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110