Deletions are marked like this. | Additions are marked like this. |
Line 2: | Line 2: |
[[V2Master]] |
V2 - Ruby on Rails Tips and Tricks
Introduction
This pages includes tips and tricks for Ruby on Rails Development.
Asset Pipeline
General documentation o the Asset Pipeline is available at:
The following applies to applications running in the production environment
Icon Display
After a production deployment, you need to check to ensure that icons are displayed correctly.
TODO: This should be part of a basic post-install smoke test. A smoke test should probably also look for fatal errors and missing file errors in the log.
Rails Applications Deployed Sub-DIrectories
If a Rails application is served from a sub-directory (e.g. v2.softxs.ch/v2p0) then you need a line like the following in config/environments/production.rb
config.action_controller.relative_url_root = '/v2p0'
If this line is missing, then image asset urls will be like :
http://v2.softxs.ch/assets/glyphicons-halflings-ab3144065a860d198f1d7d9a4882640c.png
Instead of:
http://v2.softxs.ch/v2p0/assets/glyphicons-halflings-ab3144065a860d198f1d7d9a4882640c.png
The following may be an additional way of setting a relative path (not tested):
RAILS_RELATIVE_URL_ROOT="/v2p0" bundle exec rake assets:precompile
Getting a Production Rails System to Recognize Code Changes
Due to caching, nothing happens when you change .erb (and probably other) files. In order to get the rails server to recognize changes run the following command:
touch tmp/restart.txt
You have to run the following command to get Rails to rebuild it's cached assets (stored in public/assets):
rake assets:precompile
To delete all precompiled assets: (this basically does a 'rm -rf public/assets')
rake assets:clean
In addition, the cache, located in tmp/cache, can be cleared with the folloring command (this basically does a 'rm -rf /tmp/cache/*'):
rake tmp:cache:clear
It is unlikely that you will need to run this command.
After running commands that pre-compile or clear assets, or clear the cache, don't forget to run the touch tmp/restart.txt command.
Production Asset Processing
You have two choices for asset compilation, which is defined in the file config/environments/{development|test|production}.rb
config.assets.compile = true # The setting for development and test config.assets.compile = false # The setting for production
Note that if you set production to compile assets automatically (the non-default true setting above), then you also have to change config/application.rb:
For config.assets.compile = true, e.g. 'lazy' compilation
# If you precompile assets before deploying to production, use this line # Bundler.require(*Rails.groups(:assets => %w(development test))) # If you want your assets lazily compiled in production, use this line Bundler.require(:default, :assets, Rails.env)
For config.assets.compile = false, e.g. pre-compilation
# If you precompile assets before deploying to production, use this line Bundler.require(*Rails.groups(:assets => %w(development test))) # If you want your assets lazily compiled in production, use this line # Bundler.require(:default, :assets, Rails.env)
If you select fase, then you need to pre-compile assets. Also if you empty the public/assets directory you need to pre-compile assets again. This is explained in the following article:
You pre-compile assets with the following command:
rake assets:precompile
Note that in 'real production' we do not want config.assets.compile = true, as it causes a 'Sprockets call' for each requested assets file to check if the cache is up to date. See:
You can display the asset paths, the list of all directories (including images, javascripts and stylesheets directories) from which assets will be compiled in the Rails console, using the following command:
irb(main):003:0> Rails.application.config.assets.paths => ["/v01/local/www/rails/v2p0-app/app/assets/images", "/v01/local/www/rails/v2p0-app/app/assets/javascripts", "/v01/local/www/rails/v2p0-app/app/assets/stylesheets", "/v01/local/www/rails/v2p0-app/lib/assets/images", "/v01/local/www/rails/v2p0-app/vendor/assets/javascripts", "/usr/local/lib/ruby/gems/1.9/gems/jquery-rails-2.1.4/vendor/assets/javascripts", "/usr/local/lib/ruby/gems/1.9/gems/bootstrap-sass-2.2.2.0/vendor/assets/images", "/usr/local/lib/ruby/gems/1.9/gems/bootstrap-sass-2.2.2.0/vendor/assets/javascripts", "/usr/local/lib/ruby/gems/1.9/gems/bootstrap-sass-2.2.2.0/vendor/assets/stylesheets", "/usr/local/lib/ruby/gems/1.9/gems/coffee-rails-3.2.2/lib/assets/javascripts", "/usr/local/lib/ruby/gems/1.9/gems/the_sortable_tree-2.3.0/app/assets/images", "/usr/local/lib/ruby/gems/1.9/gems/the_sortable_tree-2.3.0/app/assets/javascripts", "/usr/local/lib/ruby/gems/1.9/gems/the_sortable_tree-2.3.0/app/assets/stylesheets"]
Note that assets are taken from all the gems that are used by the application.
Bootstrap CSS Files
When using the bootstrap-sass gem, the bootstrap.css and bootstrap-responsive.css files are automatically compiled as assets, provided that app/assets/stylesheets/custom.css.scss contains:
@import "bootstrap"; @import "bootstrap-responsive";
You don't need derectives in app/assets/stylesheets/application.css. E.g. the following is unnecessary:
*= require bootstrap *= require bootstrap-responsive
Note that you don't need a copy of bootstrap-responsive.css in your codebase in app/assets (or lib/assets or vendor/assets) it is automatically included from the gem.