Ruby-on-rails 访问 rails 资产的相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11707062/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
relative paths for accessing rails assets
提问by Orlin
I'm stuck! xD I've been working on a Rails project and am having trouble accessing my assets using relative paths. A friend of mine is working on the html/css side of things while I'm handling the controllers and models. My friend recently gave me a batch of files structured in the following way:
我被卡住了!xD 我一直在研究 Rails 项目,但在使用相对路径访问我的资产时遇到了问题。当我处理控制器和模型时,我的一个朋友正在处理 html/css 方面的工作。我的朋友最近给了我一批文件结构如下:
app/assets/images/*.jpg
app/assets/stylesheets/*.css
app/assets/javascripts/*.js
app/assets/fonts/*.* (+some more css files in here)
Within my app/views/layouts directory, I have a layout named final.html.erb which is used for my entire webapp. I also have 1 page(html body content) that I'm trying to render with this layout in app/views/pages named final_page.html.erb ... the necessary routing is in place for the page to load; however, it only loads the context of the final_page.html.erb (no images, styling, or fonts). When I go to the console and type "rails server" and visit localhost:3000, the page shows up ... naked lol. The console outputs the following:
在我的 app/views/layouts 目录中,我有一个名为 final.html.erb 的布局,用于我的整个 web 应用程序。我也有 1 个页面(html 正文内容),我试图在名为 final_page.html.erb 的 app/views/pages 中使用此布局进行渲染……页面加载所需的路由已准备就绪;但是,它只加载 final_page.html.erb 的上下文(没有图像、样式或字体)。当我进入控制台并输入“rails server”并访问 localhost:3000 时,页面显示......裸体哈哈。控制台输出以下内容:
Started GET "/" for 127.0.0.1 at 2012-07-28 21:15:02 -0700
Connecting to database specified by database.yml
Processing by PagesController#final_page as HTML
Rendered pages/final_page.html.erb within layouts/final (8.4ms)
Completed 200 OK in 82ms (Views: 81.0ms | ActiveRecord: 0.0ms)
Started GET "/assets/stylesheets/style.css" for 127.0.0.1 at 2012-07-28 21:15:04 -0700
ActionController::RoutingError (No route matches [GET] "/assets/stylesheets/style.css"):
actionpack (3.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.6) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.6) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.6) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.6) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.6) lib/rails/engine.rb:479:in `call'
railties (3.2.6) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.6) lib/rails/rack/log_tailer.rb:17:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
Rendered /var/lib/gems/1.9.1/gems/actionpack-3.2.6/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (2.0ms)
I think that the problem is that I'm trying to access files using relative paths. My layout file looks like this:
我认为问题在于我正在尝试使用相对路径访问文件。我的布局文件如下所示:
<!doctype html>
<html class="no-js">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="viewport" content="width=device-width">
<title>:: Final ::</title>
<link rel="stylesheet" type="text/css" href="../../assets/stylesheets/style.css">
<link href="../../assets/stylesheets/desktop.css" rel="stylesheet" type="text/css" media="only screen and (min-width:769px) and (max-width:1000px)">
<script src="../../assets/javascripts/modernizr.js" type="text/javascript"></script>
</head>
<body>
<%= yield %>
</body>
</html>
Also, within the body of my final_page.html.erb file, I try to access images using relative paths as well...like so:
此外,在我的 final_page.html.erb 文件的正文中,我也尝试使用相对路径访问图像......像这样:
<img src="../../assets/images/mainImg.jpg" alt="img">
My friend wrote most of this html, and he has 0 experience working with rails. I decided to change the requests for assets like so:
我的朋友写了这个 html 的大部分内容,他使用 Rails 的经验为 0。我决定像这样更改对资产的请求:
From:
从:
<link rel="stylesheet" type="text/css" href="../../assets/stylesheets/style.css">
To:
到:
<%= stylesheet_link_tag "application.css" %>
From:
从:
<script src="../../assets/javascripts/modernizr.js" type="text/javascript"></script>
To:
到:
<%= javascript_include_tag "application.js" %>
From:
从:
<img src="../../assets/images/mainImg.jpg" alt="img">
To:
到:
<%= image_tag "mainImg.jpg" %>
This somewhat helps, as the images load, and very little of the styling comes through; however, it's way off from what it's supposed to look like. I'm thinking it's because my friend makes relative calls within the css files themselves:
这在一定程度上有所帮助,因为图像加载,并且很少有样式通过;然而,它与它应该看起来的样子相去甚远。我认为这是因为我的朋友自己在 css 文件中进行了相关调用:
body {
background-image: url(../images/bg.jpg);
background-repeat: repeat;
}
I've tried replacing these with url(<%= asset_path 'bg.jpg' %>) etc... but it has no effect. I've tried so many things, and have read so many posts. I'm wondering if there's a way that Rails will allow me to use relative paths. I've tried:
我试过用 url(<%= asset_path 'bg.jpg' %>) 等替换这些......但它没有效果。我尝试了很多东西,阅读了很多帖子。我想知道 Rails 是否允许我使用相对路径。我试过了:
config.assets.enabled = false
but that doesn't help ... please ... what am I doing wrong? I don't think my friend will want to stop using relative paths for his work, as it is how he does things. The site fires up well outside of Rails, but I need it to work with Rails for my webapp. Any advice would be greatly appreciated. Thank you for having the patience for reading all of this.
但这无济于事......拜托......我做错了什么?我不认为我的朋友会想停止在他的工作中使用相对路径,因为这是他做事的方式。该站点在 Rails 之外很好地启动,但我需要它与 Rails 一起用于我的 web 应用程序。任何建议将不胜感激。感谢您有耐心阅读所有这些内容。
P.S. I'm using Ruby 1.9.3, and Rails 3.2.6
PS 我使用的是 Ruby 1.9.3 和 Rails 3.2.6
回答by Danil Speransky
Try to use /assets/style.cssnot /assets/stylesheets/style.cssand so on.
尝试使用/assets/style.cssnot/assets/stylesheets/style.css等等。
If there is such structure:
如果有这样的结构:
app
assets
stylesheets
javascripts
Then to get access to files in app/assetsor in app/assets/any_folderyou should use path /assets/file.
然后要访问文件中app/assets或中的文件,app/assets/any_folder您应该使用 path /assets/file。
Updated
更新
Here try:
在这里尝试:
body {
background-image: url(bg.jpg);
background-repeat: repeat;
}
Or:
或者:
body {
background-image: url(/assets/bg.jpg);
background-repeat: repeat;
}
回答by justinlyman
Rails 3.1 introduced the new Asset Pipeline. I would recommend you read over this: http://guides.rubyonrails.org/asset_pipeline.html
Rails 3.1 引入了新的 Asset Pipeline。我建议你阅读这个:http: //guides.rubyonrails.org/asset_pipeline.html
In your app/views/layouts/application.html.erb file, make sure you have this:
在你的 app/views/layouts/application.html.erb 文件中,确保你有这个:
<%= stylesheet_link_tag "application", :media => "all" %>
In your app/assets/stylesheets/application.css file, make sure you have this line:
在你的 app/assets/stylesheets/application.css 文件中,确保你有这一行:
*= require_tree .
(This will ensure that any css files that you have in the app/assets/stylesheets directory will be available to your application)
(这将确保您在 app/assets/stylesheets 目录中的任何 css 文件都可用于您的应用程序)
in your css file you can just do this to reference images in your app/assets/images directory:
在你的 css 文件中,你可以这样做来引用你的 app/assets/images 目录中的图像:
background-image: url(bg.jpg);
回答by d1jhoni1b
In case you want to use local resources in your configuration initializersand you have this folder structure:
如果您想在配置中使用本地资源initializers并且您具有以下文件夹结构:
app
assets
stylesheets
javascripts
app
assets
stylesheets
javascripts
All you have to do is: (in this sample im using Spree base config/initializer)
您所要做的就是:(在此示例中,我使用了 Spree 基本配置/初始化程序)
Spree.config do |config|
config.admin_interface_logo = '/assets/logo/custom_admin.png'
config.logo = '/assets/logo/custom_store.jpg'
end
Spree.config do |config|
config.admin_interface_logo = '/assets/logo/custom_admin.png'
config.logo = '/assets/logo/custom_store.jpg'
end
So use /assets/your_file_or_pathand it will work :D
所以使用/assets/your_file_or_path它会起作用:D

