Reputation: 107
I've added some custom CSS and JS files to my Rails 4.1 app. The application layout page has code for a Nav bar, left hand navigation menu and a space for yield. The JS and CSS files are properly mentioned in the file and all of them are added to precompile in application.rb
<script src="assets/jquery.min.js"></script>
<!-- Bootstrap -->
<script src="assets/bootstrap.js"></script>
<!-- App -->
<script src="assets/app.js"></script>
<script src="assets/app.plugin.js"></script>
<script src="assets/jquery.slimscroll.min.js"></script>
Now, this layout works fine for the index pages. But, when I get to the show, new and edit pages, the layout breaks down. CSS or JS files in the assets directory aren't recognized.
In the terminal, I noted that when I hit a link, say, http://localhost:3000/groups/1
, I see errors like "ActionController::RoutingError (No route matches [GET] "/groups/assets/app.js")
and ActionController::RoutingError (No route matches [GET] "/groups/assets/bootstrap.css")
.
Basically, all the files mentioned in the application layout aren't recognized and there is a routing error. The same thing happens for all objects. I checked some SO solutions and all controllers inherit from ApplicationController.
Can someone let me know what's wrong? Why would the app look for assets inside every object's assets folde instead of the global location?
PS: I'm not sure what code to paste in here that'll help sort this out. Please let me know if you need to take a look at a specific page.
Upvotes: 0
Views: 86
Reputation: 23779
You are using a relative URL assets/...
, so when you access the index
page at /groups
you download assets from /groups/assets/...
.
You could instead be using an absolute path: /assets/groups/...
If the assets were downloaded correctly for the index page, they will be downloaded correctly for all pages now.
However, as @fivedigit had mentioned, you should really be using Rails asset helpers. This will set the correct URL both in development mode and when assets are compiled in production mode.
Upvotes: 2
Reputation: 18672
Rails has view helpers to properly refer to your CSS and JavaScript assets. Currently the asset path not always refers to the root directory (since it's not prefixed with a /
), which is causing issues for you.
Use helpers like these in your layout:
<%= stylesheet_link_tag 'application' %>
<%= javascript_include_tag 'application' %>
Read more about the Asset Pipeline in the Rails documentation.
Upvotes: 1