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
203771da9e
12 changed files with 64 additions and 31 deletions
|
@ -67,7 +67,7 @@ module ActionController
|
|||
# class PostsController < ApplicationController
|
||||
# REALM = "SuperSecret"
|
||||
# USERS = {"dhh" => "secret", #plain text password
|
||||
# "dap" => Digest::MD5.hexdigest(["dap",REALM,"secret"].join(":")) #ha1 digest password
|
||||
# "dap" => Digest::MD5.hexdigest(["dap",REALM,"secret"].join(":"))} #ha1 digest password
|
||||
#
|
||||
# before_filter :authenticate, :except => [:index]
|
||||
#
|
||||
|
|
|
@ -142,6 +142,8 @@ module ActionView
|
|||
# ==== Options
|
||||
# * <tt>:use_month_numbers</tt> - Set to true if you want to use month numbers rather than month names (e.g.
|
||||
# "2" instead of "February").
|
||||
# * <tt>:use_two_digit_numbers</tt> - Set to true if you want to display two digit month and day numbers (e.g.
|
||||
# "02" instead of "February" and "08" instead of "8").
|
||||
# * <tt>:use_short_month</tt> - Set to true if you want to use abbreviated month names instead of full
|
||||
# month names (e.g. "Feb" instead of "February").
|
||||
# * <tt>:add_month_numbers</tt> - Set to true if you want to use both month numbers and month names (e.g.
|
||||
|
@ -189,6 +191,10 @@ module ActionView
|
|||
# date_select("article", "written_on", :start_year => 1995, :use_month_numbers => true,
|
||||
# :discard_day => true, :include_blank => true)
|
||||
#
|
||||
# # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute,
|
||||
# # with two digit numbers used for months and days.
|
||||
# date_select("article", "written_on", :use_two_digit_numbers => true)
|
||||
#
|
||||
# # Generates a date select that when POSTed is stored in the article variable, in the written_on attribute
|
||||
# # with the fields ordered as day, month, year rather than month, day, year.
|
||||
# date_select("article", "written_on", :order => [:day, :month, :year])
|
||||
|
@ -502,6 +508,7 @@ module ActionView
|
|||
|
||||
# Returns a select tag with options for each of the days 1 through 31 with the current day selected.
|
||||
# The <tt>date</tt> can also be substituted for a day number.
|
||||
# If you want to display days with a leading zero set the <tt>:use_two_digit_numbers</tt> key in +options+ to true.
|
||||
# Override the field name using the <tt>:field_name</tt> option, 'day' by default.
|
||||
#
|
||||
# ==== Examples
|
||||
|
@ -513,6 +520,9 @@ module ActionView
|
|||
# # Generates a select field for days that defaults to the number given.
|
||||
# select_day(5)
|
||||
#
|
||||
# # Generates a select field for days that defaults to the number given, but displays it with two digits.
|
||||
# select_day(5, :use_two_digit_numbers => true)
|
||||
#
|
||||
# # Generates a select field for days that defaults to the day for the date in my_date
|
||||
# # that is named 'due' rather than 'day'.
|
||||
# select_day(my_time, :field_name => 'due')
|
||||
|
@ -532,6 +542,7 @@ module ActionView
|
|||
# want both numbers and names, set the <tt>:add_month_numbers</tt> key in +options+ to true. If you would prefer
|
||||
# to show month names as abbreviations, set the <tt>:use_short_month</tt> key in +options+ to true. If you want
|
||||
# to use your own month names, set the <tt>:use_month_names</tt> key in +options+ to an array of 12 month names.
|
||||
# If you want to display months with a leading zero set the <tt>:use_two_digit_numbers</tt> key in +options+ to true.
|
||||
# Override the field name using the <tt>:field_name</tt> option, 'month' by default.
|
||||
#
|
||||
# ==== Examples
|
||||
|
@ -559,6 +570,10 @@ module ActionView
|
|||
# # will use keys like "Januar", "Marts."
|
||||
# select_month(Date.today, :use_month_names => %w(Januar Februar Marts ...))
|
||||
#
|
||||
# # Generates a select field for months that defaults to the current month that
|
||||
# # will use keys with two digit numbers like "01", "03".
|
||||
# select_month(Date.today, :use_two_digit_numbers => true)
|
||||
#
|
||||
# # Generates a select field for months with a custom prompt. Use <tt>:prompt => true</tt> for a
|
||||
# # generic prompt.
|
||||
# select_month(14, :prompt => 'Choose month')
|
||||
|
@ -817,6 +832,9 @@ module ActionView
|
|||
# If <tt>:use_month_numbers</tt> option is passed
|
||||
# month_name(1) => 1
|
||||
#
|
||||
# If <tt>:use_two_month_numbers</tt> option is passed
|
||||
# month_name(1) => '01'
|
||||
#
|
||||
# If <tt>:add_month_numbers</tt> option is passed
|
||||
# month_name(1) => "1 - January"
|
||||
def month_name(number)
|
||||
|
@ -858,6 +876,12 @@ module ActionView
|
|||
# <option value="2">2</option>
|
||||
# <option value="3">3</option>..."
|
||||
#
|
||||
# If <tt>:use_two_digit_numbers => true</tt> option is passed
|
||||
# build_options(15, :start => 1, :end => 31, :use_two_digit_numbers => true)
|
||||
# => "<option value="1">01</option>
|
||||
# <option value="2">02</option>
|
||||
# <option value="3">03</option>..."
|
||||
#
|
||||
# If <tt>:step</tt> options is passed
|
||||
# build_options(15, :start => 1, :end => 31, :step => 2)
|
||||
# => "<option value="1">1</option>
|
||||
|
|
|
@ -334,7 +334,7 @@ module ActionView
|
|||
end.join("\n").html_safe
|
||||
end
|
||||
|
||||
# Returns a string of option tags that have been compiled by iterating over the +collection+ and assigning the
|
||||
# Returns a string of option tags that have been compiled by iterating over the +collection+ and assigning
|
||||
# the result of a call to the +value_method+ as the option value and the +text_method+ as the option text.
|
||||
# Example:
|
||||
# options_from_collection_for_select(@people, 'id', 'name')
|
||||
|
|
|
@ -219,7 +219,7 @@ module ActiveModel
|
|||
# +attribute+.
|
||||
# If no +message+ is supplied, <tt>:invalid</tt> is assumed.
|
||||
#
|
||||
# If +message+ is a symbol, it will be translated using the appropriate scope (see +translate_error+).
|
||||
# If +message+ is a symbol, it will be translated using the appropriate scope (see +generate_message+).
|
||||
# If +message+ is a proc, it will be called, allowing for things like <tt>Time.now</tt> to be used within an error.
|
||||
def add(attribute, message = nil, options = {})
|
||||
message = normalize_message(attribute, message, options)
|
||||
|
|
|
@ -41,7 +41,7 @@ module ActiveResource
|
|||
# Module to support validation and errors with Active Resource objects. The module overrides
|
||||
# Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned
|
||||
# in the web service response. The module also adds an +errors+ collection that mimics the interface
|
||||
# of the errors provided by ActiveRecord::Errors.
|
||||
# of the errors provided by ActiveModel::Errors.
|
||||
#
|
||||
# ==== Example
|
||||
#
|
||||
|
|
|
@ -100,8 +100,8 @@ class String
|
|||
#
|
||||
# +underscore+ will also change '::' to '/' to convert namespaces to paths.
|
||||
#
|
||||
# "ActiveRecord".underscore # => "active_record"
|
||||
# "ActiveRecord::Errors".underscore # => active_record/errors
|
||||
# "ActiveModel".underscore # => "active_model"
|
||||
# "ActiveModel::Errors".underscore # => "active_model/errors"
|
||||
def underscore
|
||||
ActiveSupport::Inflector.underscore(self)
|
||||
end
|
||||
|
|
|
@ -42,10 +42,10 @@ module ActiveSupport
|
|||
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
|
||||
#
|
||||
# Examples:
|
||||
# "active_record".camelize # => "ActiveRecord"
|
||||
# "active_record".camelize(:lower) # => "activeRecord"
|
||||
# "active_record/errors".camelize # => "ActiveRecord::Errors"
|
||||
# "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
|
||||
# "active_model".camelize # => "ActiveModel"
|
||||
# "active_model".camelize(:lower) # => "activeModel"
|
||||
# "active_model/errors".camelize # => "ActiveModel::Errors"
|
||||
# "active_model/errors".camelize(:lower) # => "activeModel::Errors"
|
||||
#
|
||||
# As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
|
||||
# though there are cases where that does not hold:
|
||||
|
@ -66,8 +66,8 @@ module ActiveSupport
|
|||
# Changes '::' to '/' to convert namespaces to paths.
|
||||
#
|
||||
# Examples:
|
||||
# "ActiveRecord".underscore # => "active_record"
|
||||
# "ActiveRecord::Errors".underscore # => active_record/errors
|
||||
# "ActiveModel".underscore # => "active_model"
|
||||
# "ActiveModel::Errors".underscore # => "active_model/errors"
|
||||
#
|
||||
# As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
|
||||
# though there are cases where that does not hold:
|
||||
|
|
|
@ -295,7 +295,7 @@ h4. Sprockets
|
|||
|
||||
h3. Active Record
|
||||
|
||||
* Boolean columns with 'on' and 'ON' values are type casted to true.
|
||||
* Boolean columns with 'on' and 'ON' values are type cast to true.
|
||||
|
||||
* When the +timestamps+ method creates the +created_at+ and +updated_at+ columns, it makes them non-nullable by default.
|
||||
|
||||
|
|
|
@ -614,7 +614,7 @@ As shown in the example, you can also combine standard validations with your own
|
|||
|
||||
h4. Custom Methods
|
||||
|
||||
You can also create methods that verify the state of your models and add messages to the +errors+ collection when they are invalid. You must then register these methods by using one or more of the +validate+, +validate_on_create+ or +validate_on_update+ class methods, passing in the symbols for the validation methods' names.
|
||||
You can also create methods that verify the state of your models and add messages to the +errors+ collection when they are invalid. You must then register these methods by using the +validate+ class method, passing in the symbols for the validation methods' names.
|
||||
|
||||
You can pass more than one symbol for each class method and the respective validations will be run in the same order as they were registered.
|
||||
|
||||
|
@ -637,12 +637,24 @@ class Invoice < ActiveRecord::Base
|
|||
end
|
||||
</ruby>
|
||||
|
||||
By default such validations will run every time you call +valid?+. It is also possible to control when to run these custom validations by giving an +:on+ option to the +validate+ method, with either: +:create+ or +:update+.
|
||||
|
||||
<ruby>
|
||||
class Invoice < ActiveRecord::Base
|
||||
validate :active_customer, :on => :create
|
||||
|
||||
def active_customer
|
||||
errors.add(:customer_id, "is not active") unless customer.active?
|
||||
end
|
||||
end
|
||||
</ruby>
|
||||
|
||||
You can even create your own validation helpers and reuse them in several different models. For example, an application that manages surveys may find it useful to express that a certain field corresponds to a set of choices:
|
||||
|
||||
<ruby>
|
||||
ActiveRecord::Base.class_eval do
|
||||
def self.validates_as_choice(attr_name, n, options={})
|
||||
validates attr_name, :inclusion => { {:in => 1..n}.merge(options) }
|
||||
validates attr_name, :inclusion => { { :in => 1..n }.merge!(options) }
|
||||
end
|
||||
end
|
||||
</ruby>
|
||||
|
@ -659,7 +671,7 @@ h3. Working with Validation Errors
|
|||
|
||||
In addition to the +valid?+ and +invalid?+ methods covered earlier, Rails provides a number of methods for working with the +errors+ collection and inquiring about the validity of objects.
|
||||
|
||||
The following is a list of the most commonly used methods. Please refer to the +ActiveRecord::Errors+ documentation for a list of all the available methods.
|
||||
The following is a list of the most commonly used methods. Please refer to the +ActiveModel::Errors+ documentation for a list of all the available methods.
|
||||
|
||||
h4(#working_with_validation_errors-errors). +errors+
|
||||
|
||||
|
@ -889,13 +901,8 @@ Below is a simple example where we change the Rails behavior to always display t
|
|||
|
||||
<ruby>
|
||||
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
|
||||
if instance.error_message.kind_of?(Array)
|
||||
%(#{html_tag}<span class="validation-error">
|
||||
#{instance.error_message.join(',')}</span>).html_safe
|
||||
else
|
||||
%(#{html_tag}<span class="validation-error">
|
||||
#{instance.error_message}</span>).html_safe
|
||||
end
|
||||
errors = Array(instance.error_message).join(',')
|
||||
%(#{html_tag}<span class="validation-error"> #{errors}</span>).html_safe
|
||||
end
|
||||
</ruby>
|
||||
|
||||
|
@ -949,6 +956,7 @@ h4. Creating an Object
|
|||
* +before_validation+
|
||||
* +after_validation+
|
||||
* +before_save+
|
||||
* +around_save+
|
||||
* +before_create+
|
||||
* +around_create+
|
||||
* +after_create+
|
||||
|
@ -959,6 +967,7 @@ h4. Updating an Object
|
|||
* +before_validation+
|
||||
* +after_validation+
|
||||
* +before_save+
|
||||
* +around_save+
|
||||
* +before_update+
|
||||
* +around_update+
|
||||
* +after_update+
|
||||
|
@ -967,8 +976,8 @@ h4. Updating an Object
|
|||
h4. Destroying an Object
|
||||
|
||||
* +before_destroy+
|
||||
* +after_destroy+
|
||||
* +around_destroy+
|
||||
* +after_destroy+
|
||||
|
||||
WARNING. +after_save+ runs both on create and update, but always _after_ the more specific callbacks +after_create+ and +after_update+, no matter the order in which the macro calls were executed.
|
||||
|
||||
|
@ -1013,7 +1022,7 @@ The following methods trigger callbacks:
|
|||
* +increment!+
|
||||
* +save+
|
||||
* +save!+
|
||||
* +save(false)+
|
||||
* +save(:validate => false)+
|
||||
* +toggle!+
|
||||
* +update+
|
||||
* +update_attribute+
|
||||
|
|
|
@ -71,7 +71,7 @@ person.destroy
|
|||
|
||||
h3. Validations
|
||||
|
||||
Module to support validation and errors with Active Resource objects. The module overrides Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned in the web service response. The module also adds an errors collection that mimics the interface of the errors provided by ActiveRecord::Errors.
|
||||
Module to support validation and errors with Active Resource objects. The module overrides Base#save to rescue ActiveResource::ResourceInvalid exceptions and parse the errors returned in the web service response. The module also adds an errors collection that mimics the interface of the errors provided by ActiveModel::Errors.
|
||||
|
||||
h4. Validating client side resources by overriding validation methods in base class
|
||||
|
||||
|
|
|
@ -853,7 +853,7 @@ below:
|
|||
</erb>
|
||||
|
||||
Now when you refresh the +/posts+ page, you'll see a gray background to the
|
||||
page. This same gray background will be used throughout all the views for posts.
|
||||
page. This same gray background will be used throughout all the views.
|
||||
|
||||
h4. Creating New Posts
|
||||
|
||||
|
@ -1668,7 +1668,7 @@ right in the form where you create the post. First, create a new model to hold
|
|||
the tags:
|
||||
|
||||
<shell>
|
||||
$ rails generate model tag name:string post:references
|
||||
$ rails generate model Tag name:string post:references
|
||||
</shell>
|
||||
|
||||
Again, run the migration to create the database table:
|
||||
|
|
|
@ -819,13 +819,13 @@ h5. Action View Helper Methods
|
|||
|
||||
* The +number_to_currency+, +number_with_precision+, +number_to_percentage+, +number_with_delimiter+, and +number_to_human_size+ helpers use the number format settings located in the "number":https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L2 scope.
|
||||
|
||||
h5. Active Record Methods
|
||||
h5. Active Model Methods
|
||||
|
||||
* +model_name.human+ and +human_attribute_name+ use translations for model names and attribute names if available in the "activerecord.models":https://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml#L29 scope. They also support translations for inherited class names (e.g. for use with STI) as explained above in "Error message scopes".
|
||||
|
||||
* +ActiveRecord::Errors#generate_message+ (which is used by Active Record validations but may also be used manually) uses +model_name.human+ and +human_attribute_name+ (see above). It also translates the error message and supports translations for inherited class names as explained above in "Error message scopes".
|
||||
* +ActiveModel::Errors#generate_message+ (which is used by Active Model validations but may also be used manually) uses +model_name.human+ and +human_attribute_name+ (see above). It also translates the error message and supports translations for inherited class names as explained above in "Error message scopes".
|
||||
|
||||
* +ActiveRecord::Errors#full_messages+ prepends the attribute name to the error message using a separator that will be looked up from "activerecord.errors.format.separator":https://github.com/rails/rails/blob/master/actionpack/lib/action_view/locale/en.yml#L91 (and which defaults to +' '+).
|
||||
* +ActiveModel::Errors#full_messages+ prepends the attribute name to the error message using a separator that will be looked up from "errors.format":https://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml#L4 (and which defaults to +"%{attribute} %{message}"+).
|
||||
|
||||
h5. Active Support Methods
|
||||
|
||||
|
|
Loading…
Reference in a new issue