mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge branch 'master' of github.com:lifo/docrails
This commit is contained in:
commit
e0176bf732
4 changed files with 43 additions and 21 deletions
|
@ -113,7 +113,7 @@ module AbstractController
|
|||
# helpers with the following behavior:
|
||||
#
|
||||
# String or Symbol:: :FooBar or "FooBar" becomes "foo_bar_helper",
|
||||
# and "foo_bar_helper.rb" is loaded using require_dependency.
|
||||
# and "foo_bar_helper.rb" is loaded using require_dependency.
|
||||
#
|
||||
# Module:: No further processing
|
||||
#
|
||||
|
|
|
@ -130,6 +130,7 @@ module ActionDispatch
|
|||
# * <tt>:port</tt> - Optionally specify the port to connect to.
|
||||
# * <tt>:anchor</tt> - An anchor name to be appended to the path.
|
||||
# * <tt>:trailing_slash</tt> - If true, adds a trailing slash, as in "/archive/2009/"
|
||||
# * <tt>:script_name</tt> - Specifies application path relative to domain root. If provided, prepends application path.
|
||||
#
|
||||
# Any other key (<tt>:controller</tt>, <tt>:action</tt>, etc.) given to
|
||||
# +url_for+ is forwarded to the Routes module.
|
||||
|
@ -142,6 +143,10 @@ module ActionDispatch
|
|||
# # => 'http://somehost.org/tasks/testing/'
|
||||
# url_for controller: 'tasks', action: 'testing', host: 'somehost.org', number: '33'
|
||||
# # => 'http://somehost.org/tasks/testing?number=33'
|
||||
# url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp"
|
||||
# # => 'http://somehost.org/myapp/tasks/testing'
|
||||
# url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp", only_path: true
|
||||
# # => '/myapp/tasks/testing'
|
||||
def url_for(options = nil)
|
||||
case options
|
||||
when nil
|
||||
|
|
|
@ -29,8 +29,7 @@ class Array
|
|||
#
|
||||
# [*object]
|
||||
#
|
||||
# which for +nil+ returns <tt>[nil]</tt> (Ruby 1.8.7) or <tt>[]</tt> (Ruby
|
||||
# 1.9), and calls to <tt>Array(object)</tt> otherwise.
|
||||
# which for +nil+ returns <tt>[]</tt>, and calls to <tt>Array(object)</tt> otherwise.
|
||||
#
|
||||
# Thus, in this case the behavior may be different for +nil+, and the differences with
|
||||
# <tt>Kernel#Array</tt> explained above apply to the rest of <tt>object</tt>s.
|
||||
|
|
|
@ -96,28 +96,25 @@ Assets can still be placed in the `public` hierarchy. Any assets under `public`
|
|||
|
||||
In production, Rails precompiles these files to `public/assets` by default. The precompiled copies are then served as static assets by the web server. The files in `app/assets` are never served directly in production.
|
||||
|
||||
### Controller Specific Assets
|
||||
|
||||
When you generate a scaffold or a controller, Rails also generates a JavaScript file (or CoffeeScript file if the `coffee-rails` gem is in the `Gemfile`) and a Cascading Style Sheet file (or SCSS file if `sass-rails` is in the `Gemfile`) for that controller.
|
||||
|
||||
For example, if you generate a `ProjectsController`, Rails will also add a new file at `app/assets/javascripts/projects.js.coffee` and another at `app/assets/stylesheets/projects.css.scss`. You should put any JavaScript or CSS unique to a controller inside their respective asset files, as these files can then be loaded just for these controllers with lines such as `<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag params[:controller] %>`. Note that you have to set `config.assets.precompile` in `config/environments/production.rb` if you want to precomepile them and use in production mode. You can append them one by one or do something like this:
|
||||
For example, if you generate a `ProjectsController`, Rails will also add a new file at `app/assets/javascripts/projects.js.coffee` and another at `app/assets/stylesheets/projects.css.scss`. By default these files will be ready to use by your application immediately using the `require_tree` directive. See [Manifest Files and Directives](#manifest-files-and-directives) for more details on require_tree.
|
||||
|
||||
# config/environments/production.rb
|
||||
config.assets.precompile << Proc.new { |path|
|
||||
if path =~ /\.(css|js)\z/
|
||||
full_path = Rails.application.assets.resolve(path).to_path
|
||||
app_assets_path = Rails.root.join('app', 'assets').to_path
|
||||
if full_path.starts_with? app_assets_path
|
||||
puts "including asset: " + full_path
|
||||
true
|
||||
else
|
||||
puts "excluding asset: " + full_path
|
||||
false
|
||||
end
|
||||
else
|
||||
false
|
||||
end
|
||||
}
|
||||
You can also opt to include controller specific stylesheets and JavaScript files only in their respective controllers using the following: `<%= javascript_include_tag params[:controller] %>` or `<%= stylesheet_link_tag params[:controller] %>`. Ensure that you are not using the `require_tree` directive though, as this will result in your assets being included more than once.
|
||||
|
||||
NOTE: You must have an [ExecJS](https://github.com/sstephenson/execjs#readme) supported runtime in order to use CoffeeScript. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check [ExecJS](https://github.com/sstephenson/execjs#readme) documentation to know all supported JavaScript runtimes.
|
||||
WARNING: When using asset precompilation (the production default), you will need to ensure that your controller assets will be precompiled when loading them on a per page basis. By default .coffee and .scss files will not be precompiled on their own. This will result in false positives during development as these files will work just fine since assets will be compiled on the fly. When running in production however, you will see 500 errors since live compilation is turned off by default. See [Precompiling Assets](#precompiling-assets) for more information on how precompiling works.
|
||||
|
||||
NOTE: You must have an ExecJS supported runtime in order to use CoffeeScript. If you are using Mac OS X or Windows you have a JavaScript runtime installed in your operating system. Check [ExecJS](https://github.com/sstephenson/execjs#readme) documentation to know all supported JavaScript runtimes.
|
||||
|
||||
You can also disable the generation of asset files when generating a controller by adding the following to your `config/application.rb` configuration:
|
||||
|
||||
```ruby
|
||||
config.generators do |g|
|
||||
g.assets false
|
||||
end
|
||||
```
|
||||
|
||||
### Asset Organization
|
||||
|
||||
|
@ -459,6 +456,27 @@ If you have other manifests or individual stylesheets and JavaScript files to in
|
|||
config.assets.precompile += ['admin.js', 'admin.css', 'swfObject.js']
|
||||
```
|
||||
|
||||
Or you can opt to precompile all assets with something like this:
|
||||
|
||||
```ruby
|
||||
# config/environments/production.rb
|
||||
config.assets.precompile << Proc.new { |path|
|
||||
if path =~ /\.(css|js)\z/
|
||||
full_path = Rails.application.assets.resolve(path).to_path
|
||||
app_assets_path = Rails.root.join('app', 'assets').to_path
|
||||
if full_path.starts_with? app_assets_path
|
||||
puts "including asset: " + full_path
|
||||
true
|
||||
else
|
||||
puts "excluding asset: " + full_path
|
||||
false
|
||||
end
|
||||
else
|
||||
false
|
||||
end
|
||||
}
|
||||
```
|
||||
|
||||
NOTE. Always specify an expected compiled filename that ends with js or css, even if you want to add Sass or CoffeeScript files to the precompile array.
|
||||
|
||||
The rake task also generates a `manifest.yml` that contains a list with all your assets and their respective fingerprints. This is used by the Rails helper methods to avoid handing the mapping requests back to Sprockets. A typical manifest file looks like:
|
||||
|
|
Loading…
Reference in a new issue