Relative access paths to rail resources

I am stuck! xD I am working on a Rails project and am having problems accessing my resources using relative paths. My friend is working on html / css when I process controllers and models. My friend recently gave me a package of files structured as follows:

app/assets/images/*.jpg app/assets/stylesheets/*.css app/assets/javascripts/*.js app/assets/fonts/*.* (+some more css files in here) 

In my app / views / layouts directory, I have a layout called final.html.erb that is used for all of my webapp. I also have 1 page (the contents of the html body) that I am trying to do with this layout in app / views / pages called final_page.html.erb ... the necessary routing to load the page; however, it only loads the final_page.html.erb context (no images, styles, or fonts). When I go to the console and type in the "rails" server and visit localhost: 3000, ... naked lol appears on the page. The console displays the following:


 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 the problem is that I am trying to access files using relative paths. My layout file is as follows:

 <!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, in the body of my final_page.html.erb file, I'm trying to access images using relative paths ... for example:

 <img src="../../assets/images/mainImg.jpg" alt="img"> 

My friend wrote most of this html and he has 0 experience with rails. I decided to change requests for such assets:

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 helps a little, as images are loaded and very few styles; however, this is far from what he should have looked at. I think because my friend makes relative calls inside the css files themselves:

 body { background-image: url(../images/bg.jpg); background-repeat: repeat; } 

I tried replacing them with a url (<% = asset_path 'bg.jpg'%>) etc ... but this has no effect. I tried so many things and read so many posts. I am wondering if there is a way that Rails will allow me to use relative paths. I tried:

 config.assets.enabled = false 

but it 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 he does. The site works 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 to read all of this.

PS I am using Ruby 1.9.3 and Rails 3.2.6

+6
source share
2 answers

Try using /assets/style.css not /assets/stylesheets/style.css etc.

If there is such a structure:

 app assets stylesheets javascripts 

Then, to access the files in app/assets or in app/assets/any_folder , you must use the path /assets/file .

Update

Here try:

 body { background-image: url(bg.jpg); background-repeat: repeat; } 

Or:

 body { background-image: url(/assets/bg.jpg); background-repeat: repeat; } 
+6
source

Rails 3.1 introduced the new Asset Pipeline. I would recommend you read the following: http://guides.rubyonrails.org/asset_pipeline.html

In your app / views / layouts / application.html.erb file, make sure you have this:

 <%= stylesheet_link_tag "application", :media => "all" %> 

In your app / assets / stylesheets / application.css file, make sure you have this line:

 *= require_tree . 

(This ensures that any css files that are in the app / assets / stylesheets directory will be available to your application)

in your css file, you can simply do this to link to images in the directory of your application / assets / images:

 background-image: url(bg.jpg); 
+4
source

Source: https://habr.com/ru/post/921593/


All Articles