Wondering the best way to have a link that is an image, user clicks it,
a controller is pinged, the image is flipped for another image. If the
second image is clicked vice-versa etc. Just turning something on/off
basically.
Here’s what I’ve got:
======================
<%= link_to_remote (image_tag("/images/icons/icon_unfan.png"),
:url => { :controller => “fanships”, :action => “toggle_fanship”,
:artist_id => artist.id },
:update => “need to switch image here” %>
======================
def toggle_fanship
render :template => false
@user = current_user
@artist = Artist.find(params[:artist_id])
if @artist.has_fan (@user )
destroy
else
create
end
end
======================
Unfortunately, it’s still looking for a toggle_fanship template? Anyone
know how to simple switch the image?
charley
December 7, 2009, 10:06am
2
Hi Charley Hine
You can do like
<%= link_to_remote (image_tag("/images/icons/icon_unfan.png",:id =>
"image_id",:alt => 'unfan_image'), :url => {:controller => "fanships",
:action => "toggle_fanship", :artist_id => artist.id } ) %>
Now you do what ever you do in controller. Then create
toggle_fanship.js.rjs in app/views/fanships folder and write the
following to it
page << “if($(‘image_id’).alt == ‘unfan_image’){”
page.replace_html ‘image_link_div’, :inline => “<%= link_to_remote
(image_tag(’/images/icons/icon_fan.png’,:id => ‘image_id’,:alt =>
‘fan_image’), :url => {:controller => “fanships”, :action =>
“toggle_fanship”, :artist_id => artist.id } ) %>”
page << “}”
page << “else{”
page.replace_html ‘image_link_div’, :inline => “<%= link_to_remote
(image_tag(”/images/icons/icon_unfan.png",:id => “image_id”,:alt =>
‘unfan_image’), :url => {:controller => “fanships”, :action =>
“toggle_fanship”, :artist_id => artist.id } ) %>
"
page << “}”
Here I am assuming your icons are icon_fan.png and
icon_unfan.png…Please modify the above code according to your needs
Sijo