简体   繁体   English

链接到Rails中的Assets文件夹

[英]making a link to the assets folder in rails

Wondering how I can overcome this problem. 想知道如何克服这个问题。 On the home page of my Rails app I have an image slider: 在我的Rails应用程序的主页上,有一个图像滑块:

  <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>

The images show up correctly. 图像正确显示。 Then, on another page in the same application, when I paste the same code, the images aren't showing up. 然后,在同一应用程序的另一页上,当我粘贴相同的代码时,图像没有显示出来。 In Console I get: 在控制台中,我得到:

GET http://localhost:3000/users/assets/1.1.png 404 (Not Found) 

As I'm sure you're aware, the assets folder isn't in 'Users', it's in the 'App' folder. 如您所知,资产文件夹不在“用户”中,而是在“应用程序”文件夹中。 I thought making the links like src ="../assets/..etc" would do it but not so lucky. 我以为制作像src =“ ../ assets / .. etc”这样的链接可以做到,但不是那么幸运。 I'd use Rails code, like: 我会使用Rails代码,例如:

 <%= image_tag("arrow_forward.png") %>

but this will just cause a headache as I'm using jQuery code that matches up with my img src attributes. 但这只会引起头痛,因为我正在使用与我的img src属性匹配的jQuery代码。 If it's any use, here's the jQuery code. 如果有什么用,这里是jQuery代码。 If it could be modified to hook up with the Rails image_tag way, that would be great, but I'm really curious as to why the assets folder isn't being found correctly. 如果可以对其进行修改以使其与Rails image_tag方式挂钩,那将是很好的选择,但我真的很好奇为什么找不到正确的资产文件夹。 Thanks for any help. 谢谢你的帮助。

  <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>

you have several options here... 您在这里有几种选择...

i assume you are running rails 3. i think that when you are running the asset-pipeline in production you will get digested and nondigested assets (image-HASHDIGEST.png). 我假设您正在运行Rails3。我认为,当您在生产中运行资产管道时,您将获得已消化和未消化的资产(image-HASHDIGEST.png)。 so it should be safe to use the asset-helper in your view code and hardcodes paths in your javascript. 因此在您的视图代码和JavaScript中的硬编码路径中使用资产助手应该是安全的。

since you are referring to code in <script> tags, i assume that this is some code that is also coming from your views, so you should be able to use erb syntax in there as well. 由于您是指<script>标记中的代码,因此我认为这也是您的视图中提供的一些代码,因此您也应该可以在其中使用erb语法。

when using the asset-pipeline, you can also pre-process your javascript ie with erb. 使用Asset-pipeline时,您也可以使用erb预处理javascript。 this is usually done by appending .erb to the files (some.js.erb) and then use asset-helper in there as well. 这通常是通过将.erb附加到文件(some.js.erb)上,然后在其中也使用asset-helper来完成的。 this is probably the cleanest solution. 这可能是最干净的解决方案。

despite that fact, i think the asset-pipeline sucks big times, as it introduces a whole new level of complexity to rails that's overwhelming and also over-engineered for most apps. 尽管如此,我认为资产流水线吸引了很多人,因为它给Rails引入了一个全新的高度复杂性,这对于大多数应用程序来说是压倒性的并且也是过度设计的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM