8

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.

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks you answer helps me. but you can also use '<%= ... %>' this instead '#{...}'.
0

Using

=image_tag img.image.url(:thumbnail), :class => 'button' , :onClick => "replaceImg('#{img.id}');"

can be a solution, or check the generated sourc code

2 Comments

thank you! but the key was to do it without the '' single quotes.
it's alright, but be sure your parameter is integer, because it can cause undefined variable error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.