Reputation: 8154
I'm switching off of acts_as_taggable_on because of limited flexibility for my current application. Instead, I'm building my tags from scratch as their own model. However, I already miss the "@model.tag_list" method for forms, that would split up comma-delimited user input and make individual tags. My "taggable" model is a video, and I'm curious how to write a method that can essentially act like the "tag_list"?
Example:
<%= form_for @video do %>
<%= f.text_field :tag_list %>
....
Given the input: "one, two, three"
Would build three Tags as children of the @video.
@video.tags.each do |tag|
puts tag.name
end
=> one
two
three
EDIT
I'd really like this in the Tag model, to keep form cluttering my controller. Maybe as a custom attribute? Maybe the Video model would make more sense? I know how to make a custom method to return custom data, but not assign it. Some research points me this way (not yet tested)
video.rb
def tag_list=value
value.split(',').each do |tag|
self.tags.build(:name => tag).save
end
end
Upvotes: 2
Views: 1314
Reputation: 8154
It seems like my example code ended up working
video.rb
def tag_list=value
value.split(',').each do |tag|
self.tags.build(:name => tag).save
end
end
EDIT
Also need to add to get it to work in a form:
def tag_list
self.tags.join(',')
end
Upvotes: 2
Reputation: 9529
You can leave tag_list as an attribute on Video, then in your create you could just have something like:
def create
@video = Video.new(params[:video])
if @video.save
params[:video][:tag_list].split(',').each do |tag|
@video.tags.create_by_name(tag)
end
else
render :new
end
end
Upvotes: 0