I am trying to implement a rails tagging model as outlined in Ryan Bate’s railscast #167. http://railscasts.com/episodes/167-more-on-virtual-attributes
This is a great system to use. However, I cannot get the form to submit the tag_names to the controller. The definition for tag_names is :
def tag_names
@tag_names || tags.map(&:name).join(' ')
end
Unfortunately, @tag_names never gets assigned on form submission in my case. I cannot figure out why. SO it always defaults to tags.map(&:name).join(‘ ‘). This means that I can’t create Articles because their tag_names are not there, and I also can’t edit these tags on existing ones. Anyone can help?
In short, your class is missing a setter (or in Ruby lingo, an attribute writer). There are two ways in which you can define a setter and handle converting the string of space-separated tag names into Tag objects and persist them in the database.
Solution 1 (Ryan’s solution)
In your class, define your setter using Ruby’s
attr_writermethod and convert the string of tag names (e.g."tag1 tag2 tag3") to Tag objects and save them in the database in an after save callback. You will also need a getter that converts the array ofTagobject for the article into a string representation in which tags are separated by spaces:Solution 2: Converting the string of tag names to
Tagobjects in the setter