I have implemented David Heinemeier Hansson's article in "Advanced Rails Recipes" called Toggling Attributes with AJAX. I have a question about implementing this fantastic recipe in a more general setting.
Hansson's tutorial works when toggling only one type of attribute in one view because the spinner image is named: 'spinner-#{object.id}' whereas I have multiple such toggles under matching.company_saves, matching.company_deletes, and matching.company_reads. How can I label these differently in my application helper so I'm not defining custom functions for every case?
# Hansson's application_helper.html.erb
def toggle_value(object)
remote_function(:url => url_for(object),
:method => :put,
:before => "Element.show('spinner-#{object.id}')",
:after => "Element.hide('spinner-#{object.id}')",
...etc.
# view
Save project <%= check_box_tag "@matching[candidate_stars]", "1", @matching.candidate_stars,
:onClick => toggle_value(@matching) %>
<%= image_tag "spinner.gif", :id => "spinner-#{@matching.id}",
:style => "display: none" %>
Compared to this, I would like to name the image more specifically, such as 'spinner-company_saves-#{object.id}', where company_saves is a field within object. How can I accomplish this, and how would I pass that information from the view?
Thank you!