【问题标题】:need to translate jquery code into ruby/rails需要将 jquery 代码翻译成 ruby​​/rails
【发布时间】:2013-06-14 09:42:12
【问题描述】:

在我的 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>

【问题讨论】:

    标签: jquery ruby-on-rails asset-pipeline


    【解决方案1】:

    您可以使用资产管道。

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

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

    【讨论】:

    • 谢谢。不是在主机上,但会在今天晚些时候检查一下。不明白为什么人们对我的问题投了反对票。
    猜你喜欢
    • 1970-01-01
    • 2019-03-29
    • 2010-12-24
    • 2018-05-13
    • 2020-03-02
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多