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
163258ba2a
8 changed files with 41 additions and 54 deletions
|
@ -53,6 +53,7 @@ module ActionController
|
|||
# If you need to address multiple instances of the same class in the same view, you can prefix the dom_id:
|
||||
#
|
||||
# dom_id(Post.find(45), :edit) # => "edit_post_45"
|
||||
# dom_id(Post.new, :custom) # => "custom_post"
|
||||
def dom_id(record, prefix = nil)
|
||||
if record_id = record_key_for_dom_id(record)
|
||||
"#{dom_class(record, prefix)}#{JOIN}#{record_id}"
|
||||
|
|
|
@ -409,9 +409,9 @@ cannot see application objects or methods. *Heroku requires this to be false.*
|
|||
WARNING: If you set +config.assets.initialize_on_precompile+ to false, be sure to
|
||||
test +rake assets:precompile+ locally before deploying. It may expose bugs where
|
||||
your assets reference application objects or methods, since those are still
|
||||
in scope in development mode regardless of the value of this flag. Changing this flag also effects
|
||||
in scope in development mode regardless of the value of this flag. Changing this flag also affects
|
||||
engines. Engines can define assets for precompilation as well. Since the complete environment is not loaded,
|
||||
engines (or other gems) will not be loaded which can cause missing assets.
|
||||
engines (or other gems) will not be loaded, which can cause missing assets.
|
||||
|
||||
Capistrano (v2.8.0 and above) includes a recipe to handle this in deployment. Add the following line to +Capfile+:
|
||||
|
||||
|
|
|
@ -92,7 +92,7 @@ INFO: Page caching runs in an after filter. Thus, invalid requests won't generat
|
|||
|
||||
h4. Action Caching
|
||||
|
||||
One of the issues with Page Caching is that you cannot use it for pages that require to restrict access somehow. This is where Action Caching comes in. Action Caching works like Page Caching except for the fact that the incoming web request does go from the webserver to the Rails stack and Action Pack so that before filters can be run on it before the cache is served. This allows authentication and other restriction to be run while still serving the result of the output from a cached copy.
|
||||
Page Caching cannot be used for actions that have before filters - for example, pages that require authentication. This is where Action Caching comes in. Action Caching works like Page Caching except the incoming web request hits the Rails stack so that before filters can be run on it before the cache is served. This allows authentication and other restrictions to be run while still serving the result of the output from a cached copy.
|
||||
|
||||
Clearing the cache works in a similar way to Page Caching, except you use +expire_action+ instead of +expire_page+.
|
||||
|
||||
|
|
|
@ -119,10 +119,10 @@ After you create the blog application, switch to its folder to continue work dir
|
|||
$ cd blog
|
||||
</shell>
|
||||
|
||||
The +rails new blog+ command we ran above created a folder in your working directory called <tt>blog</tt>. The <tt>blog</tt> directory has a number of auto-generated folders that make up the structure of a Rails application. Most of the work in this tutorial will happen in the <tt>app/</tt> folder, but here's a basic rundown on the function of each of the files and folders that Rails created by default:
|
||||
The +rails new blog+ command we ran above created a folder in your working directory called <tt>blog</tt>. The <tt>blog</tt> directory has a number of auto-generated files and folders that make up the structure of a Rails application. Most of the work in this tutorial will happen in the <tt>app/</tt> folder, but here's a basic rundown on the function of each of the files and folders that Rails created by default:
|
||||
|
||||
|_.File/Folder|_.Purpose|
|
||||
|app/|Contains the controllers, models, views and assets for your application. You'll focus on this folder for the remainder of this guide.|
|
||||
|app/|Contains the controllers, models, views, helpers, mailers and assets for your application. You'll focus on this folder for the remainder of this guide.|
|
||||
|config/|Configure your application's runtime rules, routes, database, and more. This is covered in more detail in "Configuring Rails Applications":configuring.html|
|
||||
|config.ru|Rack configuration for Rack based servers used to start the application.|
|
||||
|db/|Contains your current database schema, as well as the database migrations.|
|
||||
|
@ -284,13 +284,13 @@ Missing template posts/new, application/new with {:locale=>[:en], :formats=>[:ht
|
|||
|
||||
That's quite a lot of text! Let's quickly go through and understand what each part of it does.
|
||||
|
||||
The first part identifies what template is missing. In this case, it's the +posts/new+ template. Rails will first look for this template. If it can't find it, then it will attempt to load a template called +application/new+. It looks for one here because the +PostsController+ inherits from +ApplicationController+.
|
||||
The first part identifies what template is missing. In this case, it's the +posts/new+ template. Rails will first look for this template. If not found, then it will attempt to load a template called +application/new+. It looks for one here because the +PostsController+ inherits from +ApplicationController+.
|
||||
|
||||
The next part of the message contains a hash. The +:locale+ key in this hash simply indicates what spoken language template should be retrieved. By default, this is the English -- or "en" -- template. The next key, +:formats+ shows what formats of template Rails is after. The default is +:html+, and so Rails is looking for an HTML template. The final key, +:handlers+, is telling us what _template handlers_ could be used to render our template. +:erb+ is most commonly used for HTML templates, +:builder+ is used for XML templates, and +:coffee+ uses CoffeeScript to build JavaScript templates.
|
||||
The next part of the message contains a hash. The +:locale+ key in this hash simply indicates what spoken language template should be retrieved. By default, this is the English -- or "en" -- template. The next key, +:formats+ specifies the format of template to be served in response . The default format is +:html+, and so Rails is looking for an HTML template. The final key, +:handlers+, is telling us what _template handlers_ could be used to render our template. +:erb+ is most commonly used for HTML templates, +:builder+ is used for XML templates, and +:coffee+ uses CoffeeScript to build JavaScript templates.
|
||||
|
||||
The final part of this message tells us where Rails has looked for the templates. Templates within a basic Rails application like this are kept in a single location, but in more complex applications it could be many different paths.
|
||||
|
||||
The simplest template that would work in this case would be one located at +app/views/posts/new.html.erb+. The extension of this file name is key: the first extension is the _format_ of the template, and the second extension is the _handler_ that will be used. Rails is attempting to find a template called +posts/new+ within +app/views+ for the application. The format for this template can only be +html+ and the handler must be one of +erb+, +builder+ or +coffee+. Because you want to create a new HTML form, you will be using the +ERB+ language. Therefore the file should be called +posts/new.html.erb+ and be located inside the +app/views+ directory of the application.
|
||||
The simplest template that would work in this case would be one located at +app/views/posts/new.html.erb+. The extension of this file name is key: the first extension is the _format_ of the template, and the second extension is the _handler_ that will be used. Rails is attempting to find a template called +posts/new+ within +app/views+ for the application. The format for this template can only be +html+ and the handler must be one of +erb+, +builder+ or +coffee+. Because you want to create a new HTML form, you will be using the +ERB+ language. Therefore the file should be called +posts/new.html.erb+ and needs to be located inside the +app/views+ directory of the application.
|
||||
|
||||
Go ahead now and create a new file at +app/views/posts/new.html.erb+ and write this content in it:
|
||||
|
||||
|
|
|
@ -728,7 +728,7 @@ class AddFlagToProduct < ActiveRecord::Migration
|
|||
def change
|
||||
add_column :products, :flag, :boolean
|
||||
Product.all.each do |product|
|
||||
product.update_attributes!(:flag => 'false')
|
||||
product.update_attributes!(:flag => false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -804,7 +804,7 @@ class AddFlagToProduct < ActiveRecord::Migration
|
|||
end
|
||||
|
||||
def change
|
||||
add_column :products, :flag, :integer
|
||||
add_column :products, :flag, :boolean
|
||||
Product.reset_column_information
|
||||
Product.all.each do |product|
|
||||
product.update_attributes!(:flag => false)
|
||||
|
|
|
@ -148,20 +148,33 @@ You can swap an existing middleware in the middleware stack using +config.middle
|
|||
<ruby>
|
||||
# config/application.rb
|
||||
|
||||
# Replace ActionController::Failsafe with Lifo::Failsafe
|
||||
config.middleware.swap ActionController::Failsafe, Lifo::Failsafe
|
||||
# Replace ActionDispatch::ShowExceptions with Lifo::ShowExceptions
|
||||
config.middleware.swap ActionDispatch::ShowExceptions, Lifo::ShowExceptions
|
||||
</ruby>
|
||||
|
||||
h5. Middleware Stack is an Array
|
||||
|
||||
The middleware stack behaves just like a normal +Array+. You can use any +Array+ methods to insert, reorder, or remove items from the stack. Methods described in the section above are just convenience methods.
|
||||
|
||||
For example, the following removes the middleware matching the supplied class name:
|
||||
Append following lines to your application configuration:
|
||||
|
||||
<ruby>
|
||||
config.middleware.delete(middleware)
|
||||
# config/application.rb
|
||||
config.middleware.delete "Rack::Lock"
|
||||
</ruby>
|
||||
|
||||
And now if you inspect the middleware stack, you'll find that +Rack::Lock+ will not be part of it.
|
||||
|
||||
<shell>
|
||||
$ rake middleware
|
||||
(in /Users/lifo/Rails/blog)
|
||||
use ActionDispatch::Static
|
||||
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x00000001c304c8>
|
||||
use Rack::Runtime
|
||||
...
|
||||
run Myapp::Application.routes
|
||||
</shell>
|
||||
|
||||
h4. Internal Middleware Stack
|
||||
|
||||
Much of Action Controller's functionality is implemented as Middlewares. The following list explains the purpose of each of them:
|
||||
|
@ -234,32 +247,6 @@ Much of Action Controller's functionality is implemented as Middlewares. The fol
|
|||
|
||||
TIP: It's possible to use any of the above middlewares in your custom Rack stack.
|
||||
|
||||
h4. Customizing Internal Middleware Stack
|
||||
|
||||
It's possible to replace the entire middleware stack with a custom stack using <tt>ActionController::Dispatcher.middleware=</tt>.
|
||||
|
||||
Put the following in an initializer:
|
||||
|
||||
<ruby>
|
||||
# config/initializers/stack.rb
|
||||
ActionController::Dispatcher.middleware = ActionController::MiddlewareStack.new do |m|
|
||||
m.use ActionController::Failsafe
|
||||
m.use ActiveRecord::QueryCache
|
||||
m.use Rack::Head
|
||||
end
|
||||
</ruby>
|
||||
|
||||
And now inspecting the middleware stack:
|
||||
|
||||
<shell>
|
||||
$ rake middleware
|
||||
(in /Users/lifo/Rails/blog)
|
||||
use ActionController::Failsafe
|
||||
use ActiveRecord::QueryCache
|
||||
use Rack::Head
|
||||
run ActionController::Dispatcher.new
|
||||
</shell>
|
||||
|
||||
h4. Using Rack Builder
|
||||
|
||||
The following shows how to replace use +Rack::Builder+ instead of the Rails supplied +MiddlewareStack+.
|
||||
|
|
|
@ -69,6 +69,7 @@ module Rails
|
|||
# ==== Example
|
||||
#
|
||||
# add_source "http://gems.github.com/"
|
||||
#
|
||||
def add_source(source, options={})
|
||||
log :source, source
|
||||
|
||||
|
@ -82,6 +83,16 @@ module Rails
|
|||
# If options :env is specified, the line is appended to the corresponding
|
||||
# file in config/environments.
|
||||
#
|
||||
# ==== Examples
|
||||
#
|
||||
# environment do
|
||||
# "config.autoload_paths += %W(#{config.root}/extras)"
|
||||
# end
|
||||
#
|
||||
# environment(nil, :env => "development") do
|
||||
# "config.active_record.observers = :cacher"
|
||||
# end
|
||||
#
|
||||
def environment(data=nil, options={}, &block)
|
||||
sentinel = /class [a-z_:]+ < Rails::Application/i
|
||||
env_file_sentinel = /::Application\.configure do/
|
||||
|
@ -160,13 +171,13 @@ module Rails
|
|||
# <<-TASK
|
||||
# namespace :#{project} do
|
||||
# task :bootstrap do
|
||||
# puts "i like boots!"
|
||||
# puts "I like boots!"
|
||||
# end
|
||||
# end
|
||||
# TASK
|
||||
# end
|
||||
#
|
||||
# rakefile("seed.rake", "puts 'im plantin ur seedz'")
|
||||
# rakefile('seed.rake', 'puts "Planting seeds"')
|
||||
#
|
||||
def rakefile(filename, data=nil, &block)
|
||||
log :rakefile, filename
|
||||
|
@ -265,7 +276,6 @@ module Rails
|
|||
# Define log for backwards compatibility. If just one argument is sent,
|
||||
# invoke say, otherwise invoke say_status. Differently from say and
|
||||
# similarly to say_status, this method respects the quiet? option given.
|
||||
#
|
||||
def log(*args)
|
||||
if args.size == 1
|
||||
say args.first.to_s unless options.quiet?
|
||||
|
@ -276,7 +286,6 @@ module Rails
|
|||
end
|
||||
|
||||
# Add an extension to the given name based on the platform.
|
||||
#
|
||||
def extify(name)
|
||||
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
|
||||
"#{name}.bat"
|
||||
|
|
|
@ -248,7 +248,6 @@ module Rails
|
|||
|
||||
# Check whether the given class names are already taken by user
|
||||
# application or Ruby on Rails.
|
||||
#
|
||||
def class_collisions(*class_names) #:nodoc:
|
||||
return unless behavior == :invoke
|
||||
|
||||
|
@ -275,13 +274,11 @@ module Rails
|
|||
end
|
||||
|
||||
# Use Rails default banner.
|
||||
#
|
||||
def self.banner
|
||||
"rails generate #{namespace.sub(/^rails:/,'')} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]".gsub(/\s+/, ' ')
|
||||
end
|
||||
|
||||
# Sets the base_name taking into account the current class namespace.
|
||||
#
|
||||
def self.base_name
|
||||
@base_name ||= begin
|
||||
if base = name.to_s.split('::').first
|
||||
|
@ -292,7 +289,6 @@ module Rails
|
|||
|
||||
# Removes the namespaces and get the generator name. For example,
|
||||
# Rails::Generators::ModelGenerator will return "model" as generator name.
|
||||
#
|
||||
def self.generator_name
|
||||
@generator_name ||= begin
|
||||
if generator = name.to_s.split('::').last
|
||||
|
@ -304,20 +300,17 @@ module Rails
|
|||
|
||||
# Return the default value for the option name given doing a lookup in
|
||||
# Rails::Generators.options.
|
||||
#
|
||||
def self.default_value_for_option(name, options)
|
||||
default_for_option(Rails::Generators.options, name, options, options[:default])
|
||||
end
|
||||
|
||||
# Return default aliases for the option name given doing a lookup in
|
||||
# Rails::Generators.aliases.
|
||||
#
|
||||
def self.default_aliases_for_option(name, options)
|
||||
default_for_option(Rails::Generators.aliases, name, options, options[:aliases])
|
||||
end
|
||||
|
||||
# Return default for the option name given doing a lookup in config.
|
||||
#
|
||||
def self.default_for_option(config, name, options, default)
|
||||
if generator_name and c = config[generator_name.to_sym] and c.key?(name)
|
||||
c[name]
|
||||
|
@ -331,14 +324,12 @@ module Rails
|
|||
end
|
||||
|
||||
# Keep hooks configuration that are used on prepare_for_invocation.
|
||||
#
|
||||
def self.hooks #:nodoc:
|
||||
@hooks ||= from_superclass(:hooks, {})
|
||||
end
|
||||
|
||||
# Prepare class invocation to search on Rails namespace if a previous
|
||||
# added hook is being used.
|
||||
#
|
||||
def self.prepare_for_invocation(name, value) #:nodoc:
|
||||
return super unless value.is_a?(String) || value.is_a?(Symbol)
|
||||
|
||||
|
@ -354,7 +345,6 @@ module Rails
|
|||
|
||||
# Small macro to add ruby as an option to the generator with proper
|
||||
# default value plus an instance helper method called shebang.
|
||||
#
|
||||
def self.add_shebang_option!
|
||||
class_option :ruby, :type => :string, :aliases => "-r", :default => Thor::Util.ruby_command,
|
||||
:desc => "Path to the Ruby binary of your choice", :banner => "PATH"
|
||||
|
|
Loading…
Reference in a new issue