简体   繁体   中英

(Rails) asset from asset pipeline shows up in index.html.erb file, but not show.html.erb

I am trying to get both an image and a CSS style from my asset pipeline ni rails 4. I have an index.html.erb and a show.html.erb. I have the exact same code in each, but it works in the index.html file, but not in the show.html file.

Code:

 <!-- Styles -->
<link rel="stylesheet" href="assets/styles-bluegreen.css" id="theme-styles">

 <a href="index.html" id="logo">
     <img src="assets/my-logo.png" alt="My Blog">
 </a>

I have this exact same code, because I copied and pasted (yes, I know it's against rails dry policy). I originally had this in a _header.html.erb partial, and the code did not work in the partial either, so i put the exact code in each file, and it works in the index.thml.erb, but no in the show.html.erb. What could be the reason?

Note, I have also tried:

<link rel="stylesheet" href="stylesheets/styles-bluegreen.css" id="theme-styles">

and

<link rel="stylesheet" href="assets/stylesheets/styles-bluegreen.css" id="theme-styles">

Here is my asset pipeline:

资产管道

If you copy-paste the snippet, there's no reason for them to behave differently except one of your view (index or show) is cached and it is served to the client directly instead of being rendered. Can you check if this is the case?

Btw, you really should use stylesheet_link_tag because links to assets might differ from development to production environments. This helper renders those links correspondingly to environments.

isn't it href="<%= asset_path('styles-bluegreen.css')%>"

And then for the picture src="<%= asset_path('my-logo.png')%>"

Also, you'd have to add .erb to the end of the show.html filename (so show.html.erb)

That usually works for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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