So stuck, yet easy problem

Hi,

I’ve been working on a simple but useful application that uses draggable Ajax tags to associate photos with people. I have the tags working and the photo list working, but I cannot seem make add the tagging association in the database.

The relevant code is posted below. My guess is that it has something to do with the PhotosController, although I don’t know anymore…I’ve tried every combination of code I could think of. I really don’t want to give up on this project, so I would be so grateful for your help!

Thanks,

Michael

this might take a big rewrite, since I’ve had 2000 versions of it PhotosController …

def addtag @person_id = params[:id].split(“_”)[1] @photo_id = params[:photo]

@photo = Photo.find(@photo_id)
@photo.people = @person_id
if @photo.update_attributes()
  flash[:notice] = 'Success!!'
  redirect_to :action => 'show', :id => photo

end

end …

tagging.rhtml (views) ## page where the tagging is done … <% for photo in @photos %> <div id=“<%= “photo_#{photo.id }” %>” style=“float:left; padding: 0 5 5 0;”> <%= link_to(image_tag(“photos/#{photo.thumbnail}”, :size => ‘200’, :border => 0,

            :id => 'thumbnail'),
            url_for(:action => 'show', :id => photo)
        )
        %>
        <div id="indicator" style="display:none;margin-top:0px;">

            <%= image_tag "indicator.gif" %> Updating...
        </div>
        <%= drop_receiving_element("photo_#{[photo.id](http://photo.id)}",
            :url => {:controller => "photos", :action => "addtag"},

            :photo => "#{[photo.id](http://photo.id)}",  ## added this because I'm not sure the Controller knows both ids of dropped tag and photo being tagged
            :loading => "

Element.show(‘indicator’)", :complete => “Element.hide(‘indicator’)”) %>

    </div>

<% end %> …

class Person < ActiveRecord::Base has_and_belongs_to_many :photos End

class Photo < ActiveRecord::Base validates_presence_of :filename

has_and_belongs_to_many :people End

This is what my database looks like:

Table “photos” “filename”, :string “description”, :text “thumbnail”, :string “date_taken”, :date

Table “people_photos”

“person_id”, :integer “person_id”, :integer

Table “people” “firstname”, :string “lastname”, :string “email”, :string “school”, :string

Table “people_photos”

“person_id”, :integer “person_id”, :integer

should be

person_id photo_id

if this was only a typo you should add the error stack trace.

Cheers, Jan

@photo.people = @person_id     if @photo.update_attributes()

These two lines should be:

@photo.people << Person.find(@person_id) if @photo.save

unless I'm misunderstanding your original code...

Jeff

Thanks! It works! The '<<' was the breakthrough. I also needed to pass-back the id of the photo where the tag was dropped; this was done using :where. Here's the updated relevant working code.

The only line that doesn't work is @photo.save, which doesn't then flash "Success!!". When I added an else statement, it also didn't evaluate. Not sure what's going on there...

Thanks again!

## tagging.rhtml ... <div id="indicator" style="display:none;margin-top:0px;">                 <%= image_tag "indicator.gif" %> Updating...             </div>             <%= drop_receiving_element("photo_#{photo.id}",                 :url => {:controller => "photos", :action => "addtag", :where => "#{photo.id}"},                 :loading => "Element.show('indicator')",                 :complete => "Element.hide('indicator')") %>

## PhotosController ...   def addtag     @person_id = params[:id].split("_")[1]     @photo_id = params[:where]     @photo = Photo.find(@photo_id)     @photo.people << Person.find(@person_id)     if @photo.save       flash[:notice] = 'Success!!'       redirect_to :action => 'taggable'     end   end

MichaelM wrote:

The only line that doesn't work is @photo.save, which doesn't then flash "Success!!". When I added an else statement, it also didn't evaluate. Not sure what's going on there...

Thanks again!

Hmmm.... Does the redirect to the 'taggable' action work? If so, it's probably just a bug in how you're trying to display the flash notice. Can you post taggable.rhtml?

If the redirect is not working either, then something else is going on, like an exception is being raised somewhere that's causing the entire method to abort. Try enclosing the whole method body in a begin/rescue/end structure, and write something to log each step of the way, until you can see what's going on.

Jeff softiesonrails.com

Below is my 'taging.rhtml'. I'm actually not quite sure how to do begin-rescue-end you suggested. I've been struggling with this for so long, I started another post.

http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/7193bcc39ac82a01/044cae6079b7b382?lnk=raot#044cae6079b7b382

No luck yet, but I'll be sure to post the solutions once I figure it out (that is, IF I figure it out!)

# tagging.rhtml <h1>Tagging photos</h1>

<!-- Placeholder for @PHOTOS_ACROSS funcationality --> <% @PHOTOS_ACROSS = 2 %>

<div id="listBox" style="width:150px;">     <% for person in @all_people %>         <span id="<%= "person_#{person.id}" %>" class="nameSpan" style="border:1px solid #ffffee;display:block"><%= person.firstname + " " + person.lastname %></span>         <%= draggable_element ("person_#{person.id}", :revert => true) %>     <% end %> </div>

<p> <div id="indicator" style="display:none;margin-top:0px;">                 <%= image_tag "indicator.gif" %> Updating... </div> </p>

<div>       <% for photo in @photos %>         <div id="<%= "photo_#{photo.id}" %>" style="float:left;padding: 0 5 5 0; border:thin solid #000">             <%= link_to(image_tag("photos/#{photo.thumbnail}",                 :size => '200',                 :border => 0,                 :id => 'thumbnail'),                 url_for(:action => 'show', :id => photo)             )             %>

            <%= drop_receiving_element("photo_#{photo.id}",                 :url => {:controller => "photos", :action => "addtag", :where => "#{photo.id}"},                 :loading => "Element.show('indicator')",                 :complete => "Element.hide('indicator')")             %>

            <div id="listing">                 <%= render :partial => "taglist", :locals => { :photo_tags => photo.people.find(:all) } %>             </div>

        </div>     <% end %>

    <%= link_to 'Previous page', { :page => @photo_pages.current.previous } if @photo_pages.current.previous %>     <%= link_to 'Next page', { :page => @photo_pages.current.next } if @photo_pages.current.next %> </div>

MichaelM wrote:

Below is my 'taging.rhtml'. I'm actually not quite sure how to do begin-rescue-end you suggested. I've been struggling with this for so long, I started another post.

http://groups.google.com/group/rubyonrails-talk/browse_frm/thread/7193bcc39ac82a01/044cae6079b7b382?lnk=raot#044cae6079b7b382

I'll address the flash issue here, and the other issue in the other thread...

Unless I'm missing it, I don't see anywhere in your tagging.html that actually displays the flash notice.

<% if flash[:notice] %> <h3><% flash[:notice] %></h3> <% end %>

Obviously the <h3> is just an example, you can also use a div and style it with css.

Jeff softiesonrails.com

I posted that exact code in tagging.rhtml, but it again doesn't display. It continues to display 'Success!' if I click to another page. Do I need an if statment do display flash[:notice]? None of my other (working) pages have it.