8

抱歉——我是一个使用 Ruby on Rails 的新手。仍然对它的工作原理有些困惑。

现在,在我看来,在我的滚动 div 下,我有以下代码:

#scroller
 -@other_images.each do |img|
  .thumbnail_wrapper
    .thumbnail_image
      =image_tag img.image.url(:thumbnail), :class => 'button' , :onClick => "replaceImg(img.id);"

@other_images 是一个变量,其中包含我想在页面上显示的所有缩略图。单击一个将在其他地方用自己的大图像刷新 div。

对应的js函数为:

:javascript
 function replaceImg(id){
  var url = '/images/refresh/' + id;
  new Ajax.Updater('content_image', url);
 }

如果我只写一个有效的网址,这将有效。但是将参数“id”传递给 js 函数不起作用。我不知所措......我错过了什么?

如何将这个 rails 变量 -- img --- 传递到我的 js 中?它只是停留在那个循环中。

任何帮助将非常感激。

4

2 回答 2

6

试试这个:

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.

于 2010-11-27T10:00:24.227 回答
0

Using

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

can be a solution, or check the generated sourc code

于 2010-11-27T10:11:50.433 回答