Apologies -- I'm a newbie using Ruby on Rails. Still a little confused about how it works.
Right now, in my view, under my scroller div, I have this code:
#scroller
-@other_images.each do |img|
.thumbnail_wrapper
.thumbnail_image
=image_tag img.image.url(:thumbnail), :class => 'button' , :onClick => "replaceImg(img.id);"
@other_images is a variable that holds all the thumbnail images I want to display on the page. Clicking on one will refresh a div elsewhere with its own big image.
the corresponding js function is:
:javascript
function replaceImg(id){
var url = '/images/refresh/' + id;
new Ajax.Updater('content_image', url);
}
This works if I just write in a valid url. But passing the param "id" into the js function does not work. I'm at a loss... what am I missing?
How do I pass this rails variable -- img --- into my js? It's just stuck in that loop.
Any help would be really appreciated.
Try this:
img.image.url(:thumbnail), :class => 'button' , :onClick => "replaceImg(#{img.id});"
The #{...}
construct executes the ruby code within the curly brackets and replaces the entire construct with the result. Can be used anywhere you want to replace part of a string with some content from a ruby variable or method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With