Rails 3.1 and Image Assets

后端 未结 7 1934
深忆病人
深忆病人 2020-11-30 16:53

I have put all my images for my admin theme in the assets folder within a folder called admin. Then I link to it like normal ie.

# Ruby    
image_tag \"admin         


        
相关标签:
7条回答
  • 2020-11-30 17:10

    The asset pipeline in rails offers a method for this exact thing.

    You simply add image_path('image filename') to your css or scss file and rails takes care of everything. For example:

    .logo{ background:url(image_path('admin/logo.png'));

    (note that it works just like in a .erb view, and you don't use "/assets" or "/assets/images" in the path)

    Rails also offers other helper methods, and there's another answer here: How do I use reference images in Sass when using Rails 3.1?

    0 讨论(0)
  • 2020-11-30 17:12

    For what it's worth, when I did this I found that no folder should be include in the path in the css file. For instance if I have app/assets/images/example.png, and I put this in my css file...

    div.example { background: url('example.png'); }
    

    ... then somehow it magically works. I figured this out by running the rake assets:precompile task, which just sucks everything out of all your load paths and dumps it in a junk drawer folder: public/assets. That's ironic, IMO...

    In any case this means you don't need to put any folder paths, everything in your assets folders will all end up living in one huge directory. How this system resolves file name conflicts is unclear, you may need to be careful about that.

    Kind of frustrating there aren't better docs out there for this big of a change.

    0 讨论(0)
  • 2020-11-30 17:23

    You'll want to change the extension of your css file from .css.scss to .css.scss.erb and do:

    background-image:url(<%=asset_path "admin/logo.png"%>);
    

    You may need to do a "hard refresh" to see changes. CMD+SHIFT+R on OSX browsers.

    In production, make sure

    rm -rf public/assets    
    bundle exec rake assets:precompile RAILS_ENV=production
    

    happens upon deployment.

    0 讨论(0)
  • 2020-11-30 17:28

    http://railscasts.com/episodes/279-understanding-the-asset-pipeline

    This railscast (Rails Tutorial video on asset pipeline) helps a lot to explain the paths in assets pipeline as well. I found it pretty useful, and actually watched it a few times.

    The solution I chose is @Lee McAlilly's above, but this railscast helped me to understand why it works. Hope it helps!

    0 讨论(0)
  • 2020-11-30 17:30

    In 3.1 you just get rid of the 'images' part of the path. So an image that lives in /assets/images/example.png will actually be accessible in a get request at this url - /assets/example.png

    Because the assets/images folder gets generated along with a new 3.1 app, this is the convention that they probably want you to follow. I think that's where image_tag will look for it, but I haven't tested that yet.

    Also, during the RailsConf keynote, I remember D2h saying the the public folder should not have much in it anymore, mostly just error pages and a favicon.

    0 讨论(0)
  • 2020-11-30 17:31

    In rails 4 you can now use a css and sass helper image-url:

    div.logo {background-image: image-url("logo.png");}
    

    If your background images aren't showing up consider looking at how you're referencing them in your stylesheets.

    0 讨论(0)
提交回复
热议问题