22

I want to create a new folder in app/assets, in this folder I have some file, each file have css folder and javascript folder, like the below given addresses:

app/assets/plugins/bootstrap/css/bootstrap.min.css app/assets/plugins/bootstrap/js/bootstrap.min.js

I use below code to define css file, but not true and rails cannot find and load bootstrap.min.css:

<%= stylesheet_link_tag "bootstrap.min", media: "all", "data-turbolinks-track" => true %>

<%= stylesheet_link_tag "../plugins/bootstrap/css/bootstrap.min", media: "all", "data-turbolinks-track" => true %>

How can I declare css and javascript from plugin folder?

Note: When I want see the css file in browser by this address:

http://localhost:3000/assets/plugins/bootstrap/css/bootstrap.min.css

I get below error: No route matches [GET] "/assets/plugins/bootstrap/css/bootstrap.min.css"

But for css file that exist in assets/stylesheets/ I can see css file in browser by enter address like top. Why the diffrent beetwin stylesheets and plugins directory?

Rajesh Omanakuttan
  • 6,788
  • 7
  • 47
  • 85
mgh
  • 921
  • 3
  • 9
  • 37
  • Normally when all the assets that are added to asset paths can only be viewed in browser through the address `http://localhost:3000/assets/bootstrap.min.css` and not through `http://localhost:3000/assets/plugins/bootstrap/css/bootstrap.min.css`. Why you want such a behaviour? – Rajesh Omanakuttan Apr 21 '14 at 09:38

1 Answers1

29

In application.rb, add the below lines:

config.assets.enabled = true
config.assets.paths << Rails.root.join("app", "assets", "plugins", "bootstrap","css")
config.assets.paths << Rails.root.join("app", "assets", "plugins", "bootstrap","js")

Then you will be able to access bootstrap.min.css and bootstrap.min.js in your browser,

i.e., http://localhost:3000/assets/bootstrap.min.css / http://localhost:3000/assets/bootstrap.min.js

That means you have added the above to the asset paths.

Now you can call it using stylesheet_link_tag or javascript_include_tag.

Hope it helps :)

Rajesh Omanakuttan
  • 6,788
  • 7
  • 47
  • 85
  • Yes, I try it and work. I have many directory in `plugins`. For each one I have to add a expression in `application.rb`?!! Do you know a simple way to add all directory in `application.rb`? – mgh Apr 21 '14 at 09:56
  • 1
    If you want to maintain plugins separately and under different folders, then you need to add it separately. Otherwise, maintain all plugin JS file under `app/assets/plugins/bootstrap/js` and CSS files under `app/assets/plugins/bootstrap/css`. So that would be good to go. – Rajesh Omanakuttan Apr 21 '14 at 10:01
  • It worked for me in development mode. It is not working when I deploy to the server. Please have a look at this question: http://stackoverflow.com/questions/34466781/rails-rails-root-join-misbehaving-after-deployment – Aleksandrus Dec 25 '15 at 22:12