app/assets/images/*.jpg app/assets/stylesheets/*.css app/assets/javascripts/*.js app/assets/fonts/*.* (+some more css files in here)
在我的app / views / layouts目录中,我有一个名为final.html.erb的布局,用于我的整个webapp.我还有1页(html正文内容),我正在尝试使用名为final_page.html.erb的app / views / pages中的此布局进行渲染…必要的路由到位才能加载页面;但是,它只加载final_page.html.erb的上下文(没有图像,样式或字体).当我进入控制台并键入“rails server”并访问localhost:3000时,页面显示…裸体lol.控制台输出以下内容:
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)
我认为问题是我正在尝试使用相对路径访问文件.我的布局文件如下所示:
<!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>
另外,在我的final_page.html.erb文件的正文中,我尝试使用相对路径访问图像……就像这样:
<img src="../../assets/images/mainImg.jpg" alt="img">
我的朋友写了大部分这个HTML,他有使用rails的经验.我决定更改资产请求,如下所示:
从:
<link rel="stylesheet" type="text/css" href="../../assets/stylesheets/style.css">
至:
<%= stylesheet_link_tag "application.css" %>
从:
<script src="../../assets/javascripts/modernizr.js" type="text/javascript"></script>
至:
<%= javascript_include_tag "application.js" %>
从:
<img src="../../assets/images/mainImg.jpg" alt="img">
至:
<%= image_tag "mainImg.jpg" %>
这有点帮助,因为图像加载,并且很少有造型通过;然而,它远离它应该看起来的样子.我在想它是因为我的朋友在css文件中自己进行相对调用:
body { background-image: url(../images/bg.jpg); background-repeat: repeat; }
我尝试用url替换它们(<%= asset_path'bg.jpg'%>)等等……
但它没有效果.我已经尝试了很多东西,并且阅读了很多帖子.我想知道Rails是否允许我使用相对路径.我试过了:
config.assets.enabled = false
但这没有帮助……拜托……我做错了什么?我不认为我的朋友会想要停止使用相对路径来完成他的工作,因为这是他做事的方式.该网站在Rails之外启动很好,但我需要它与Rails一起使用我的webapp.任何建议将不胜感激.感谢您耐心阅读所有这些内容.
附:我正在使用Ruby 1.9.3和Rails 3.2.6
解决方法
如果有这样的结构:
app assets stylesheets javascripts
然后,要访问app / assets或app / assets / any_folder中的文件,您应该使用path / assets / file.
更新
在这里尝试:
body { background-image: url(bg.jpg); background-repeat: repeat; }
要么:
body { background-image: url(/assets/bg.jpg); background-repeat: repeat; }