-2

在我的 Rails 应用程序中,仅当代码在我的主页上时才会显示图像(在我的 routes.rb 中有 root to: 的那个)

<div id = "carousel">
    <img id ="rotate_images" src="assets/1.1.png"/>

    <div id = "back">
    <img src = "assets/arrow_back.png">
  </div>  

    <div id = "forward">
    <img src = "assets/arrow_forward.png"></div>
  </div>
</div>

当我在我的应用程序的其他页面上使用上面的代码时,我收到错误 404 - 找不到文件。它找不到资产文件夹的正确位置。但是,当我使用 rails 代码链接到图像时,它在我的应用程序中的任何地方都可以使用:

<div id = "carousel">
    <%= image_tag "1.1.png", :id => "rotate_images" %>

    <div id = "back">
      <%= image_tag "arrow_back.png" %>
  </div>  

    <div id = "forward">
      <%= image_tag "arrow_forward.png" %>
    </div>

</div>

所以我认为最好将我的 jquery 图像滑块更改为 ruby​​/rails 代码。可惜,因为我花了很多时间制作它。有人可以将下面的 jquery 代码翻译成 ruby​​/rails 或者让我走上正确的轨道吗?谢谢你的帮助。

<script>

$('#back').on({
      'click': function () {
          var origsrc = $(rotate_images).attr('src');
          var src = '';
          if (origsrc == 'assets/3.1.png') src = 'assets/2.1.png';
          if (origsrc == 'assets/2.1.png') src = 'assets/1.1.png';
          if (origsrc == 'assets/1.1.png') src = 'assets/1.1.png';
          $(rotate_images).attr('src', src);
      }
});

$('#forward').on({
      'click': function () {
          var origsrc = $(rotate_images).attr('src');
          var src = '';
          if (origsrc == 'assets/1.1.png') src = 'assets/2.1.png';
          if (origsrc == 'assets/2.1.png') src = 'assets/3.1.png';
          if (origsrc == 'assets/3.1.png') src = 'assets/3.1.png';
          $(rotate_images).attr('src', src);
      }
});
</script>
4

1 回答 1

1

您可以使用资产管道。

 if (origsrc == '<%= asset_path('1.1.pg') %>') src = '<%= asset_path('2.1.png') %>';

src =您可以在所有线路上使用此 ruby​​ 注入。

于 2013-06-14T09:53:36.570 回答