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)
)
%>
<%= image_tag “indicator.gif” %> Updating…
<%= drop_receiving_element(“photo_#{photo.id}”,
:url => {:controller => “photos”, :action => “addtag”},
:photo => “#{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