在public/或app/assets/中使用Rails 4静态资产 [英] Rails 4 static assets in public/ or app/assets/

查看:159
本文介绍了在public/或app/assets/中使用Rails 4静态资产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这是一个简单问题的冗长内容,但是我想弄清楚我的想法.

Sorry if this is a lengthy buildup to a simple question, but I wanted to get my thoughts clear.

我现在已经在几个项目中使用了Rails 4,并且一直在使用image_tag('/assets/image.png')来解决资产路径助手在Rails 4中的工作方式的变化.直到今天,我决定了解更多有关Rails 4的知识.进行更改,并在链轮-rails 中找到了第一个更改说明.我还注意到,Rails帮助器中/actionview/lib/action_view/helpers/asset_url_helper.rb#L170中的ASSET_PUBLIC_DIRECTORIES仅指向公用文件夹.对我来说,很明显,如果您要访问静态文件,Rails希望您使用公用文件夹.

I've used Rails 4 on a couple projects now and I've been using image_tag('/assets/image.png') to get around the changes to how asset path helpers work in Rails 4. That is until today when I decided to learn more about the changes and found this first change note in sprockets-rails. I've also noted that the ASSET_PUBLIC_DIRECTORIES in /actionview/lib/action_view/helpers/asset_url_helper.rb#L170 in Rails helpers only point to public folders. It became pretty obvious to me that if you are accessing static files, Rails wants you to use use the public folder.

因此,现在我已经弄清楚了,我只是不明白为什么 edge rails文档清楚地说明了这一点:

So now that I've figured this out, I just can't understand why the edge rails docs clearly state this:

在以前的Rails版本中,所有资产都位于公共的子目录中,例如图像,javascript和样式表.通过资产管道,这些资产的首选位置现在是app/assets目录.

In previous versions of Rails, all assets were located in subdirectories of public such as images, javascripts and stylesheets. With the asset pipeline, the preferred location for these assets is now the app/assets directory.

image_path实际上会为public/images文件夹生成一个uri,但这是完全相反的.

image_path in practice generates a uri to the public/images folder wich is totally contrary.

总而言之,我确实需要使用所有可用的帮助程序和摘要构建器,因为最终我要使用 asset_sync .

And to sum this all up I do need to use all available helpers and digest builders, because I end up deploying my assets to S3 with asset_sync.

所以我的问题是公正的;是否有一个正确的位置来放置图像/未编译的资产并使用asset_path帮助器?所有文档以及所有其他有关堆栈溢出的讨论都与使用app/assets文件夹的人员有关,但是sprockets-rails希望我们将public用于非消化性资产.

So my question is just; is there a correct place to put images/non-compiled assets and use asset_path helpers? All the documentation and every other stack overflow conversation is about people using the app/assets folder, but sprockets-rails wants us to use public for non-digest assets. Are the docs and info on the web just in need of an update, or are others just making due with prepending all asset paths with /assets/?

更新:我认为我确实确实遇到了一个问题,即我没有重新启动开发服务器,并且应用程序/资产/图像中的图像没有显示出来,因此会退回到公众手中.另请注意,我在回形针default_url中使用了资产帮助器(被称为指向我发现的多个堆栈溢出答案中的资产的方法,但是,在回形针中使用带有插值选项的资产路径帮助器也将回退给公众,因为显然不会在现有文件中找到未插值的资产名称.

推荐答案

使用rake assets:precompile时,Rails将所有资产从app/assets文件夹移至public/assets.这是因为普通浏览器无法访问您的app目录,但是可以访问public.

When you use rake assets:precompile, Rails moves all of the assets from your app/assets folder, to public/assets. This is as a normal browser does not have access to your app directory, but does have access to public.

因此,为回答您的问题,这是我的想法.如果您的图像用于网站布局,例如徽标或背景,则应将其存储在app/assets/images目录中,但是,如果图像是用户生成的,例如个人资料图像,则应将它们直接存储在诸如public/images.理想情况下,您可以将它们存储在S3或CDN中,但是您已经在这样做了.

So, to answer your question, here are my thoughts. If your images are for your sites layout e.g logos or backgrounds, then you should store them in the app/assets/images directory, however, if the images are user generated, such as profile images, then these should be stored directly in something such as public/images. Ideally you would store those in S3 or a CDN, but you're already doing that.

作为奖励,Rails图像上传工具Carrierwave使用public/images类型的路径作为其图像的默认存储,如您在其配置文件中所见:

As a bonus, Carrierwave, a Rails image upload gem, uses a public/images type path as the default store for its images, as you can see in their config file:

    # Override the directory where uploaded files will be stored.
    # This is a sensible default for uploaders that are meant to be mounted:
     def store_dir
     "images/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
     end

希望这会有所帮助!

这篇关于在public/或app/assets/中使用Rails 4静态资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆