简体   繁体   English

Rails 3.1 和图像资源

[英]Rails 3.1 and Image Assets

I have put all my images for my admin theme in the assets folder within a folder called admin.我已将我的管理主题的所有图像放在名为 admin 的文件夹中的 assets 文件夹中。 Then I link to it like normal ie.然后我像正常一样链接到它,即。

# Ruby    
image_tag "admin/file.jpg" .....
#CSS
.logo{ background:url('/assets/images/admin/logo.png');

FYI.供参考。 Just for testing I am not using the asset_path tag just yet as I have not compiled my assets.只是为了测试我还没有使用asset_path标签,因为我还没有编译我的资产。

Ok all good so far until I decided to update an image.好吧,到目前为止一切都很好,直到我决定更新图像。 I replaced some colors but on reload the new styled image is not showing.我更换了一些 colors 但重新加载时未显示新样式的图像。 If I view the image directly in the browser its still showing the old image.如果我直接在浏览器中查看图像,它仍然显示旧图像。 Going one step further I destroyed the admin images folder.更进一步,我销毁了管理图像文件夹。 But it has broken nothing all the images are still being displayed.但它没有破坏所有图像仍在显示。 And yes I have cleared my cache and have tried on multiple browsers.是的,我已经清除了缓存并尝试了多个浏览器。

Is there some sort of image caching going on?是否正在进行某种图像缓存? This is just local development using pow to serve the pages.这只是使用 pow 服务页面的本地开发。

Even destroying the whole images folder the images are still being served.即使破坏了整个图像文件夹,图像仍然被提供。

Am I missing something?我错过了什么吗?

In 3.1 you just get rid of the 'images' part of the path.在 3.1 中,您只需去掉路径的“图像”部分。 So an image that lives in /assets/images/example.png will actually be accessible in a get request at this url - /assets/example.png因此,位于/assets/images/example.png中的图像实际上可以在此 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.因为assets/images文件夹是与新的 3.1 应用程序一起生成的,所以这是他们可能希望您遵循的约定。 I think that's where image_tag will look for it, but I haven't tested that yet.我认为那是image_tag会寻找它的地方,但我还没有测试过。

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.另外,在 RailsConf 主题演讲中,我记得 D2h 说public folder不应该再有太多内容了,主要是错误页面和网站图标。

You'll want to change the extension of your css file from .css.scss to .css.scss.erb and do:您需要将 css 文件的扩展名从.css.scss.css.scss.erb并执行以下操作:

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. OSX 浏览器上的 CMD+SHIFT+R。

In production, make sure在生产中,确保

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

happens upon deployment.在部署时发生。

For what it's worth, when I did this I found that no folder should be include in the path in the css file.对于它的价值,当我这样做时,我发现 css 文件的路径中不应包含任何文件夹。 For instance if I have app/assets/images/example.png , and I put this in my css file...例如,如果我有app/assets/images/example.png ,我把它放在我的 css 文件中......

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 .我通过运行rake assets:precompile任务解决了这个问题,它只是从所有加载路径中吸取所有内容并将其转储到垃圾抽屉文件夹中: public/assets That's ironic, IMO...讽刺的是,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.有点令人沮丧的是,没有更好的文档来应对这么大的变化。

In rails 4 you can now use a css and sass helper image-url:在 rails 4 中,您现在可以使用 css 和 sass 帮助程序图像网址:

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.如果您的背景图像没有出现,请考虑查看您在样式表中如何引用它们。

when referencing images in CSS or in an IMG tag, use image-name.jpg在 CSS 或 IMG 标签中引用图像时,使用 image-name.jpg

while the image is really located under./assets/images/image-name.jpg而图像实际上位于 ./assets/images/image-name.jpg 下

http://railscasts.com/episodes/279-understanding-the-asset-pipeline 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.这个 railscast(关于资产管道的 Rails 教程视频)也有助于解释资产管道中的路径。 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.我选择的解决方案是上面的@Lee McAlilly,但是这个 railscast 帮助我理解了它的工作原理。 Hope it helps!希望能帮助到你!

The asset pipeline in rails offers a method for this exact thing. rails 中的资产管道为这个确切的事情提供了一种方法。

You simply add image_path('image filename') to your css or scss file and rails takes care of everything.您只需将 image_path('image filename') 添加到 css 或 scss 文件中,rails 会处理一切。 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) (请注意,它就像在 a.erb 视图中一样工作,并且您不要在路径中使用“/assets”或“/assets/images”)

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? Rails 还提供了其他辅助方法,这里还有另一个答案: How do I use reference images in Sass when using Rails 3.1?

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

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