add_tag method errors
Forums > Book - Chapter 10 > add_tag method errors
| Author | Message |
|---|---|
|
jjkiesch Member since 22 Jul 23:52 0 posts |
I kept getting “undefined method ’+’” when trying to add tags to the photos. for some reason it doesn’t like the line: |
|
alan Member since 26 Jun 23:06 0 posts |
Just looking at this – it looks like the tagging library has gone through some updates… I’ll add a page describing what I’ve found, but in the meantime, your fix works, as does |
|
Herb Taylor Member since 05 Aug 04:13 0 posts |
Alan - |
|
kevox Member since 11 Jul 11:49 0 posts |
I had to use ’@photo.tag_list.names << params:tag’ because when I just used :tag, I was getting a comment added to the tag along the lines of Map!WashHandsWithIndifference or something like that… |
|
kevox Member since 11 Jul 11:49 0 posts |
grr… |
|
alan Member since 26 Jun 23:06 0 posts |
Try putting your code inside |
|
wlockhart Member since 13 Jul 09:22 0 posts |
Hi All, Even after downloading the latest version (dated November 23, 2007) of the source code from Google Code [ http://code.google.com/p/railscoders-net/downloads/list ] the ‘add a tag’ code still wasn’t working. I eventually managed to get it to work by changing the file ‘app/controllers/user_photos_controller.rb’ as follows:
def add_tag
@photo = @logged_in_user.photos.find(params[:id])
# changed to reflect latest version of acts_as_taggable_on_steroids
@photo.tag_list.add(params[:tag][:name])
if @photo.save
@new_tag = @photo.reload.tags.find_by_name params[:tag][:name]
else
render :nothing => true
end
end
In particular, changing the following line:
@photo.tag_list.add(params[:tag][:name])
Hope this helps someone out there. Kind Regards Walter |
|
kaplotnik Member since 02 Dec 07:23 0 posts |
Or, more simply:
@photo.tag_list << params[:tag][:name]
|
|
kaplotnik Member since 02 Dec 07:23 0 posts |
I went with this add_tag code (hoping pre tags
do_update = false
if (tag_name = params[:tag][:name].strip).blank?
# Ignore empty tags
elsif @photo.tag_list.index(tag_name)
# It's already there
else
@photo.tag_list << tag_name
if @photo.save
@new_tag = @photo.reload.tags.find_by_name(tag_name)
do_update = true
end
end
if !do_update
render :nothing => true
end
|