提交 0245792c 编写于 作者: X Xavier Noria

Merge branch 'master' of github.com:lifo/docrails

......@@ -174,7 +174,7 @@ def count
to_a.size
end
# Returns true if there are any errors, false if not.
# Returns true if no errors are found, false otherwise.
def empty?
all? { |k, v| v && v.empty? }
end
......
......@@ -30,6 +30,8 @@ def slice!(*keys)
omit
end
# Removes and returns the key/value pairs matching the given keys.
# {:a => 1, :b => 2, :c => 3, :d => 4}.extract!(:a, :b) # => {:a => 1, :b => 2}
def extract!(*keys)
result = {}
keys.each {|key| result[key] = delete(key) }
......
......@@ -81,13 +81,13 @@ h3. Railties
* jQuery and Prototype are no longer vendored and is provided from now on by the jquery-rails and prototype-rails gems.
* The application generator accepts an option -j which can be an arbitrary string. If passed "foo", the gem "foo-rails" is added to the Gemfile, and the application JavaScript manifest requires "foo" and "foo_ujs". Currently only "prototype-rails" and "jquery-rails" exist and provide those files via the asset pipeline.
* The application generator accepts an option +-j+ which can be an arbitrary string. If passed "foo", the gem "foo-rails" is added to the +Gemfile+, and the application JavaScript manifest requires "foo" and "foo_ujs". Currently only "prototype-rails" and "jquery-rails" exist and provide those files via the asset pipeline.
* Generating an application or a plugin runs +bundle install+ unless --skip-gemfile or --skip-bundle is specified.
* Generating an application or a plugin runs +bundle install+ unless +--skip-gemfile+ or +--skip-bundle+ is specified.
* The controller and resource generators will now automatically produce asset stubs (this can be turned off with --skip-assets). These stubs will use CoffeeScript and Sass, if those libraries are available.
* The controller and resource generators will now automatically produce asset stubs (this can be turned off with +--skip-assets+). These stubs will use CoffeeScript and Sass, if those libraries are available.
* Scaffold and app generators use the Ruby 1.9 style hash when running on Ruby 1.9. To generate old style hash, --old-style-hash can be passed.
* Scaffold and app generators use the Ruby 1.9 style hash when running on Ruby 1.9. To generate old style hash, +--old-style-hash+ can be passed.
* Scaffold controller generator creates format block for JSON instead of XML.
......@@ -109,15 +109,15 @@ h4. Action Controller
* A warning is given out if the CSRF token authenticity cannot be verified.
* Specify +force_ssl+ in a controller to force the browser to transfer data via HTTPS protocol on that particular controller. To limit to specific actions, :only or :except can be used.
* Specify +force_ssl+ in a controller to force the browser to transfer data via HTTPS protocol on that particular controller. To limit to specific actions, +:only+ or +:except+ can be used.
* Sensitive query string parameters specified in <tt>config.filter_parameters</tt> will now be filtered out from the request paths in the log.
* URL parameters which return nil for +to_param+ are now removed from the query string.
* URL parameters which return +nil+ for +to_param+ are now removed from the query string.
* Added <tt>ActionController::ParamsWrapper</tt> to wrap parameters into a nested hash, and will be turned on for JSON request in new applications by default. This can be customized by setting <tt>ActionController::Base.wrap_parameters</tt> in <tt>config/initializer/wrap_parameters.rb</tt>.
* Added <tt>config.action_controller.include_all_helpers</tt>. By default <tt>helper :all</tt> is done in <tt>ActionController::Base</tt>, which includes all the helpers by default. Setting +include_all_helpers+ to false will result in including only application_helper and the helper corresponding to controller (like foo_helper for foo_controller).
* Added <tt>config.action_controller.include_all_helpers</tt>. By default <tt>helper :all</tt> is done in <tt>ActionController::Base</tt>, which includes all the helpers by default. Setting +include_all_helpers+ to +false+ will result in including only application_helper and the helper corresponding to controller (like foo_helper for foo_controller).
* +url_for+ and named url helpers now accept +:subdomain+ and +:domain+ as options.
......@@ -166,16 +166,18 @@ end
<ruby>
class PostsController < ActionController::Base
stream :only => :index
stream
end
</ruby>
Please read the docs at "<tt>ActionController::Streaming</tt>":http://edgeapi.rubyonrails.org/classes/ActionController/Streaming.html for more information.
You can restrict it to some actions by using +:only+ or +:except+. Please read the docs at "<tt>ActionController::Streaming</tt>":http://edgeapi.rubyonrails.org/classes/ActionController/Streaming.html for more information.
* The redirect route method now also accepts a hash of options which will only change the parts of the url in question, or an object which responds to call, allowing for redirects to be reused.
h4. Action Dispatch
* <tt>config.action_dispatch.x_sendfile_header</tt> now defaults to +nil+ and <tt>config/environments/production.rb</tt> doesn't set any particular value for it. This allows servers to set it through <tt>X-Sendfile-Type</tt>.
* <tt>ActionDispatch::MiddlewareStack</tt> now uses composition over inheritance and is no longer an array.
* Added <tt>ActionDispatch::Request.ignore_accept_header</tt> to ignore accept headers.
......@@ -200,7 +202,7 @@ h4. Action View
* +file_field+ automatically adds <tt>:multipart => true</tt> to the enclosing form.
* Added a convenience idiom to generate HTML5 data-* attributes in tag helpers from a :data hash of options:
* Added a convenience idiom to generate HTML5 data-* attributes in tag helpers from a +:data+ hash of options:
<plain>
tag("div", :data => {:name => 'Stephen', :city_state => %w(Chicago IL)})
......@@ -219,7 +221,7 @@ Keys are dasherized. Values are JSON-encoded, except for strings and symbols.
* The submit form helper does not generate an id "object_name_id" anymore.
* Allows <tt>FormHelper#form_for</tt> to specify the :method as a direct option instead of through the :html hash. <tt>form_for(@post, remote: true, method: :delete)</tt> instead of <tt>form_for(@post, remote: true, html: { method: :delete })</tt>.
* Allows <tt>FormHelper#form_for</tt> to specify the +:method+ as a direct option instead of through the +:html+ hash. <tt>form_for(==@==post, remote: true, method: :delete)</tt> instead of <tt>form_for(==@==post, remote: true, html: { method: :delete })</tt>.
* Provided <tt>JavaScriptHelper#j()</tt> as an alias for <tt>JavaScriptHelper#escape_javascript()</tt>. This supersedes the <tt>Object#j()</tt> method that the JSON gem adds within templates using the JavaScriptHelper.
......@@ -250,7 +252,7 @@ user.build_account{ |a| a.credit_limit => 100.0 }
* CSV Fixtures are deprecated and support will be removed in Rails 3.2.0.
* <tt>ActiveRecord#new</tt>, <tt>ActiveRecord#create</tt> and <tt>ActiveRecord#update_attributes</tt> all accept a second hash as an option that allows you to specify which role to consider when assigning attributes. This is built on top of ActiveModel's new mass assignment capabilities:
* <tt>ActiveRecord#new</tt>, <tt>ActiveRecord#create</tt> and <tt>ActiveRecord#update_attributes</tt> all accept a second hash as an option that allows you to specify which role to consider when assigning attributes. This is built on top of Active Model's new mass assignment capabilities:
<ruby>
class Post < ActiveRecord::Base
......@@ -284,7 +286,7 @@ People.limit(1).offset(1).count # => 'SELECT COUNT(*) FROM people LIMIT 1 OFFSET
* <tt>ActiveRecord::Associations::AssociationProxy</tt> has been split. There is now an +Association+ class (and subclasses) which are responsible for operating on associations, and then a separate, thin wrapper called +CollectionProxy+, which proxies collection associations. This prevents namespace pollution, separates concerns, and will allow further refactorings.
* Singular associations (has_one, belongs_to) no longer have a proxy and simply returns the associated record or nil. This means that you should not use undocumented methods such as bob.mother.create - use bob.create_mother instead.
* Singular associations (+has_one+, +belongs_to+) no longer have a proxy and simply returns the associated record or +nil+. This means that you should not use undocumented methods such as +bob.mother.create+ - use +bob.create_mother+ instead.
* Support the <tt>:dependent</tt> option on <tt>has_many :through</tt> associations. For historical and practical reasons, +:delete_all+ is the default deletion strategy employed by <tt>association.delete(*records)</tt>, despite the fact that the default strategy is +:nullify+ for regular has_many. Also, this only works at all if the source reflection is a belongs_to. For other situations, you should directly modify the through association.
......@@ -341,7 +343,7 @@ has_many :things, :conditions => 'foo = #{bar}' # before
has_many :things, :conditions => proc { "foo = #{bar}" } # after
</ruby>
Inside the proc, 'self' is the object which is the owner of the association, unless you are eager loading the association, in which case 'self' is the class which the association is within.
Inside the proc, +self+ is the object which is the owner of the association, unless you are eager loading the association, in which case +self+ is the class which the association is within.
You can have any "normal" conditions inside the proc, so the following will work too:
<ruby>
......@@ -366,7 +368,9 @@ end
* Calling <tt>ActiveRecord::Base#clone</tt> will result in a shallow copy of the record, including copying the frozen state. No callbacks will be called.
* Calling <tt>ActiveRecord::Base#dup</tt> will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return true for <tt>new_record?</tt>, have a nil id field, and is saveable.
* Calling <tt>ActiveRecord::Base#dup</tt> will duplicate the record, including calling after initialize hooks. Frozen state will not be copied, and all associations will be cleared. A duped record will return +true+ for <tt>new_record?</tt>, have a +nil+ id field, and is saveable.
* The query cache now works with prepared statements. No changes in the applications are required.
h3. Active Model
......@@ -380,6 +384,8 @@ h3. Active Model
* Added support for selectively enabling and disabling observers.
* Alternate <tt>I18n</tt> namespace lookup is no longer supported.
h3. Active Resource
* The default format has been changed to JSON for all requests. If you want to continue to use XML you will need to set <tt>self.format = :xml</tt> in the class. For example,
......
......@@ -242,11 +242,11 @@ end
h5. Sending Email To Multiple Recipients
It is possible to send email to one or more recipients in one email (for e.g. informing all admins of a new signup) by setting the list of emails to the <tt>:to</tt> key. The <tt>to:</tt> key however expects a string so you have join the list of recipients using a comma.
It is possible to send email to one or more recipients in one email (for e.g. informing all admins of a new signup) by setting the list of emails to the <tt>:to</tt> key. The list of emails can be an array of email addresses or a single string with the addresses separated by commas.
<ruby>
class AdminMailer < ActionMailer::Base
default :to => Admin.all.map(&:email).join(", "),
default :to => Admin.all.map(&:email),
:from => "notification@example.com"
def new_registration(user)
......@@ -256,6 +256,8 @@ It is possible to send email to one or more recipients in one email (for e.g. in
end
</ruby>
The same format can be used to set carbon copy (Cc:) and blind carbon copy (Bcc:) recipients, by using the <tt>:cc</tt> and <tt>:bcc</tt> keys respectively.
h5. Sending Email With Name
Sometimes you wish to show the name of the person instead of just their email address when they receive the email. The trick to doing that is
......
......@@ -751,8 +751,6 @@ class Person < ActiveRecord::Base
end
</ruby>
h4. +errors.clear+
The +clear+ method is used when you intentionally want to clear all the messages in the +errors+ collection. Of course, calling +errors.clear+ upon an invalid object won't actually make it valid: the +errors+ collection will now be empty, but the next time you call +valid?+ or any method that tries to save this object to the database, the validations will run again. If any of the validations fail, the +errors+ collection will be filled again.
......@@ -799,6 +797,7 @@ h3. Displaying Validation Errors in the View
Rails maintains an official plugin that provides helpers to display the error messages of your models in your view templates. You can install it as a plugin or as a Gem.
h4. Installing as a plugin
<shell>
$ rails plugin install git://github.com/joelmoss/dynamic_form.git
</shell>
......@@ -806,6 +805,7 @@ $ rails plugin install git://github.com/joelmoss/dynamic_form.git
h4. Installing as a Gem
Add this line in your Gemfile:
<ruby>
gem "dynamic_form"
</ruby>
......
......@@ -2095,6 +2095,30 @@ shape_types = [Circle, Square, Triangle].sample(2)
NOTE: Defined in +active_support/core_ext/array/random_access.rb+.
h4. Adding Elements
h5. +prepend+
This method is an alias of <tt>Array#unshift</tt>.
<ruby>
%w(a b c d).prepend('e') # => %w(e a b c d)
[].prepend(10) # => [10]
</ruby>
NOTE: Defined in +active_support/core_ext/array/prepend_and_append.rb+.
h5. +append+
This method is an alias of <tt>Array#<<</tt>.
<ruby>
%w(a b c d).append('e') # => %w(a b c d e)
[].append([1,2]) # => [[1,2]]
</ruby>
NOTE: Defined in +active_support/core_ext/array/prepend_and_append.rb+.
h4. Options Extraction
When the last argument in a method call is a hash, except perhaps for a +&block+ argument, Ruby allows you to omit the brackets:
......@@ -2722,6 +2746,18 @@ hash # => {:a => 1}
NOTE: Defined in +active_support/core_ext/hash/slice.rb+.
h4. Extracting
The method +extract!+ removes and returns the key/value pairs matching the given keys.
<ruby>
hash = {:a => 1, :b => 2}
rest = hash.extract!(:a) # => {:a => 1}
hash # => {:b => 2}
</ruby>
NOTE: Defined in +active_support/core_ext/hash/slice.rb+.
h4. Indifferent Access
The method +with_indifferent_access+ returns an +ActiveSupport::HashWithIndifferentAccess+ out of its receiver:
......
......@@ -557,7 +557,7 @@ The error occurred while evaluating nil.each
*+set_load_path+* This initializer runs before +bootstrap_hook+. Adds the +vendor+, +lib+, all directories of +app+ and any paths specified by +config.load_paths+ to +$LOAD_PATH+.
*+set_autoload_path+* This initializer runs before +bootstrap_hook+. Adds all sub-directories of +app+ and paths specified by +config.autoload_paths+ to +ActiveSupport::Dependencies.autoload_paths+.
*+set_autoload_paths+* This initializer runs before +bootstrap_hook+. Adds all sub-directories of +app+ and paths specified by +config.autoload_paths+ to +ActiveSupport::Dependencies.autoload_paths+.
*+add_routing_paths+* Loads (by default) all +config/routes.rb+ files (in the application and railties, including engines) and sets up the routes for the application.
......
......@@ -190,7 +190,8 @@ aliases = {
"g" => "generate",
"c" => "console",
"s" => "server",
"db" => "dbconsole"
"db" => "dbconsole",
"r" => "runner"
}
command = ARGV.shift
......
......@@ -1179,14 +1179,14 @@ On pages generated by +NewsController+, you want to hide the top menu and add a
<% end %>
<% content_for :content do %>
<div id="right_menu">Right menu items here</div>
<%= yield(:news_content) or yield %>
<%= content_for?(:news_content) ? yield(:news_content) : yield %>
<% end %>
<%= render :template => 'layouts/application' %>
</erb>
That's it. The News views will use the new layout, hiding the top menu and adding a new right menu inside the "content" div.
There are several ways of getting similar results with different sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the +ActionView::render+ method via +render :template => 'layouts/news'+ to base a new layout on the News layout. If you are sure you will not subtemplate the +News+ layout, you can replace the +yield(:news_content) or yield+ with simply +yield+.
There are several ways of getting similar results with different sub-templating schemes using this technique. Note that there is no limit in nesting levels. One can use the +ActionView::render+ method via +render :template => 'layouts/news'+ to base a new layout on the News layout. If you are sure you will not subtemplate the +News+ layout, you can replace the +content_for?(:news_content) ? yield(:news_content) : yield+ with simply +yield+.
h3. Changelog
......
......@@ -471,8 +471,8 @@ By default migrations tell you exactly what they're doing and how long it took.
Several methods are provided that allow you to control all this:
* +suppress_messages+ suppresses any output generated by its block
* +say+ outputs text (the second argument controls whether it is indented or not)
* +suppress_messages+ takes a block as an argument and suppresses any output generated by the block.
* +say+ takes a message argument and outputs it as is. A second boolean argument can be passed to specify whether to indent or not.
* +say_with_time+ outputs text along with how long it took to run its block. If the block returns an integer it assumes it is the number of rows affected.
For example, this migration
......@@ -510,7 +510,7 @@ generates the following output
20080906170109 CreateProducts: migrated (10.0097s)
</shell>
If you just want Active Record to shut up then running +rake db:migrate VERBOSE=false+ will suppress any output.
If you just want Active Record to shut up then running +rake db:migrate VERBOSE=false+ will suppress all output.
h3. Using Models in Your Migrations
......
......@@ -148,7 +148,7 @@ The above creates +lib/tasks/bootstrap.rake+ with a +boot:strap+ rake task.
h4. generate(what, args)
Runs the supplied rails generator with given arguments. For example, I love to scaffold some whenever I’m playing with Rails:
Runs the supplied rails generator with given arguments.
<ruby>
generate(:scaffold, "person", "name:string", "address:text", "age:number")
......@@ -176,12 +176,6 @@ You can also run rake tasks with a different Rails environment:
rake "db:migrate", :env => 'production'
</ruby>
Or even use sudo:
<ruby>
rake "gems:install", :sudo => true
</ruby>
h4. route(routing_code)
This adds a routing entry to the +config/routes.rb+ file. In above steps, we generated a person scaffold and also removed +public/index.html+. Now to make +PeopleController#index+ as the default page for the application:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册