Got it!!! Here are the key steps:
The view should decide which image to present based on the info in the
order.placed_date column and link it remotely to the toggle action.
Don't mind the span tags, they are there just to hide some text which
can be used for sorting (can't really sort off an image, right?).
View:
<td id=<%= order.id %> colspan=1 align="center">
<% if order.placed_date == nil then %>
<span class="hidden">nil</span><div id="image"><%= link_to_remote
image_tag("unselected.png"), :url => { :controller =>
"orders", :action => "toggle", :id => order } %></div>
<% else %>
<span class="hidden">sel</span><div id="image"><%= link_to_remote
image_tag("selected.png"), :url => { :controller => "orders", :action
=> "toggle", :id => order } %></div>
<% end %></td>
The toggle method updates the database and toggles between rjs files
Order:
def toggle
@order = Order.find(params[:id])
if @order.placed_date == nil
@order.placed_date = Date.today
respond_to do |format|
format.js {render :action => "selectedGraphicClicked.rjs"}
end
else
@order.placed_date = nil
respond_to do |format|
format.js {render :action => "unselectedGraphicClicked.rjs"}
end
end
@order.save
end
end
The rjs file identifies the id to replace using the @order.id.to_s and
replaces the html with a new link_to_remote as shown below:
rjs file 1:
[email protected]_s].replace_html link_to_remote(image_tag
("selected.png"), :url => { :controller => "orders", :action =>
"toggle", :id => @order })
rjs file2:
[email protected]_s].replace_html link_to_remote(image_tag
("unselected.png"), :url => { :controller => "orders", :action =>
"toggle", :id => @order })
This code can likely be cleaned up quite a bit but I am going to enjoy
clicking the buttons and watching the server update the database, and
the javascript executing seamlessly for a while before I change
anything. Thanks for the responses.
Tim
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---