Rails - Understanding application.js and application.css

New to the rails. Just try to understand these two files in the \ assests directory.

For example, the application.js file has lines such as:

//= require jquery //= require jquery_ujs //= require_tree . 

I understand that require_tree . just adds all the js files to the current directory. And in context, I can say that require jquery adds jQuery libraries. But where do they get these jQuery libraries from? I do not see jquery.js files in the folder with my assets - or in my entire application directly?

Similarly, I follow the twitter bootstrap installation instructions ( http://rubydoc.info/gems/bootstrap-sass-rails/2.3.2.0/frames ). After adding my gems to the Gemfile, I need to add //= require bootstrap to the application.js file and @import 'bootstrap' to my application.css, and now it magically works !!! What for!? I can not find these files anywhere

Thanks!

+4
source share
4 answers

To understand this, you should look at Sprockets , which is used to compile and maintain web resources.

You can find these files using gem which . Here is an example with bootstrap-sass :

 ~ gem which bootstrap-sass /Users/andr/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/bootstrap-sass-2.3.0.1/lib/bootstrap-sass.rb 

Explanation of @import 'bootstrap' : https://github.com/thomas-mcdonald/bootstrap-sass#css is an open issue with comments.

+2
source

application.css and application.js are not regular css and js files (they can be, but they serve a different purpose

both are manifest files that describe the asset pipeline along with asterisks for js

therefore, as Michael Darrant correctly pointed out, http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives will be the right place

but according to your other questions, it seems that you are missing a pretty critical piece of the puzzle

Rails mainly work with precious stones. Gems are pieces of ruby ​​code that you can tell to add rails to your application through bundler

when you add a gem like a self-tuning gem, it is installed (by default, in the stone library where you have the ruby ​​installed - something like Ruby193 \ lib \ ruby ​​\ gems \ 1.9.1 \ gems)

If you go there and look at the bootstrap gem, you will find the css and js files that are included in the application, as well as the jquery and jquery_ujs that you included in the manifest file

since gems are placed next to the rails, the rails do not mind where the files are (as long as they know where they are).

So, the manifest file tells the rails "Hey, include this file for me in this particular order" That's why you can include the files you wrote that are in the resources folder and the files are included in the gem

If you do not include the files in the manifest, but still install a gem equivalent to writing a css or js file, placing it in some folder and not reporting that it exists. When you tell the rails where it is through the manifest file, it will include it in the asset compilation process, and you can access it regularly.

Alternatively, you do not need to use the asset pipeline for assets

you can include css and js file with normal

  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 

and just place the files somewhere and point them to the files, but the resource pipeline has many advantages, and it really makes your life easier when you recognize it.

+9
source

I think he gets them from the gems that are installed on your system and included in this project through your Gemfile.

You can find out at http://guides.rubyonrails.org/asset_pipeline.html#manifest-files-and-directives

0
source

He solves magic! Thus, you cannot see the files in your own project directory. If you're really interested, you can check out the code for the gem at the gem github page https://github.com/twbs/bootstrap

In addition, if you really want to change something in the gem, you can develop the code in your own github, change the settings in your local branch, and then specify your own github as the source for the gem in your gemfile, for example .. .

gem 'twitter-bootstrap': git => 'git @ github.com: my_github / twitter_bootstrap.git'

But you might not want to try it if you are really new to rails;)

0
source

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


All Articles