0

I'm new to rails, I have created an app in which user has a profile and that can be edited with edit profile option.

In controller I have,

ProfilesController

In Views I have,

index.html.erb
edit.html.erb

It all works fine for index page, but for the edit page it's cool for first load and then after refreshing it, the css files are not loading.

url looks like http://localhost:3000/profiles/25/edit

Everything works right for index page no matter how many refreshes, but it's disappointing for edit page.I have inspected elements in browser, it says GET http://localhost:3000/profiles/25/assets/bootstrap.min.css [HTTP/1.1 404 Not Found 650ms]

i.e it searches for files in localhost:3000/profiles/25/assets/ which doesn't exists instead of searching in localhost:3000/assets/


EDIT :

Structure:

app/assets/stylesheets
application.css
bootstrap.min.css
style.css

In application.html.erb , I link application.css as,

<%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>

and other css files as

<link href="assets/style.css" rel="stylesheet">
<link href="assets/bootstrap.min.css" rel="stylesheet">

Am I correct with structure and linking? If not then where to place and use external css files.

Please help!! Thanks in advance.

5
  • How are you loading bootstrap? Commented Apr 29, 2015 at 16:34
  • I feel weird with the path of assets profiles/25/assets/bootstrap.css, can you show us the code? Commented Apr 29, 2015 at 16:52
  • @narush can you please post your project/app structure. Commented Apr 29, 2015 at 17:10
  • @infused in application.html <link href="assets/bootstrap.min.css" rel="stylesheet"> .It's working fine on first load, but doesn't load on refreshing edit page Commented Apr 29, 2015 at 19:47
  • @anonymousxxx it's the GET request made by browser, I found that one after inspecting element.It should be /assets/bootstrap.css instead. Commented Apr 29, 2015 at 19:55

1 Answer 1

2

The default Rails asset pipeline assumes that you want all your stylesheets to apply to every page. I found this awkward at first but quickly realized that it fits well with Rails' ethos of "Don't repeat yourself" and creating sensible defaults to save the developer time. Most of the time, the styles you're writing won't mind being present on all pages.

From what I'm hearing, one page in your app loads fine but another one fails because the CSS asset isn't found. This suggests to me that you're doing something apart from Rails' asset pipeline, and consequently bootstrap.css isn't getting loaded incorrectly. Can you search your app/assets and app/views directories for all mentions of bootstrap, and let me know what you find?

Ideally the only place the Bootstrap styles should be declared would be in the app/assets/stylesheets/application.css "manifest file" which looks something like this:

/*
 * This is a Sprockets manifest. See https://github.com/sstephenson/sprockets
 * It compiles all CSS for the old site design (pre-WPBoom redesign).
 *
 *= require bootstrap
 *= require jquery-ui
 *= require_tree .
 */
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response! Can you please tell me how to link external css(eg. bootstrap.css and style.css) files in application.html.erb . I have put them in /assets/stylesheets ?
@narush you have to store external css (third party) into vendor directory, /vendor/assets/stylesheets
@narush it's worth reading Rails' asset pipeline guide. This section explains how to include a specific asset in your layout template, but if possible you should try to keep all your styles in application.css and then only include that single file in your layout. Once you get used to this it makes for WAY less work in the long run.
Thanx man! It worked! .... worth reading pipeline guide.We need to link ext. css by <%= stylesheet_link_tag "filename" %> rather than <link href=""> to dynamically link them even after refreshing.
Ah yes when linking to assets in Rails (even image URLs in <img tags! Even image URLs mentioned in your CSS!) you should use Rails' built-in helpers such as image_tag (in an ERB template) or image-url (in CSS) rather than providing the path directly, because depending on your deployment environment config, specifying the direct path may cause all your asset links to break, leaving your live site imageless.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.