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

Conflicts:
	actionmailer/lib/action_mailer/base.rb
	activesupport/lib/active_support/core_ext/kernel/requires.rb
This commit is contained in:
Xavier Noria 2011-05-25 22:48:47 +02:00
commit 00e1d0832e
94 changed files with 406 additions and 361 deletions

View File

@ -1,30 +1,35 @@
== Welcome to Rails
Rails is a web-application framework that includes everything needed to create
database-backed web applications according to the Model-View-Control pattern.
database-backed web applications according to the Model-View-Controller pattern.
This pattern splits the view (also called the presentation) into "dumb"
templates that are primarily responsible for inserting pre-built data in between
HTML tags. The model contains the "smart" domain objects (such as Account,
Product, Person, Post) that holds all the business logic and knows how to
persist themselves to a database. The controller handles the incoming requests
(such as Save New Account, Update Product, Show Post) by manipulating the model
and directing data to the view.
Understanding the MVC pattern is key to understanding Rails. MVC divides your application
into three layers, each with a specific responsibility.
In Rails, the model is handled by what's called an object-relational mapping
layer entitled Active Record. This layer allows you to present the data from
database rows as objects and embellish these data objects with business logic
methods. You can read more about Active Record in its
The View layer is composed of "templates" that are responsible for providing
appropriate representations of your application's resources. Templates
can come in a variety of formats, but most view templates are HTML with embedded Ruby
code (.erb files).
The Model layer represents your domain model (such as Account, Product, Person, Post)
and encapsulates the business logic that is specific to your application. In Rails,
database-backed model classes are derived from ActiveRecord::Base. ActiveRecord allows
you to present the data from database rows as objects and embellish these data objects
with business logic methods. Although most Rails models are backed by a database, models
can also be ordinary Ruby classes, or Ruby classes that implement a set of interfaces as
provided by the ActiveModel module. You can read more about Active Record in its
{README}[link:files/activerecord/README_rdoc.html].
The controller and view are handled by the Action Pack, which handles both
layers by its two parts: Action View and Action Controller. These two layers
are bundled in a single package due to their heavy interdependence. This is
unlike the relationship between the Active Record and Action Pack that is much
more separate. Each of these packages can be used independently outside of
Rails. You can read more about Action Pack in its
{README}[link:files/actionpack/README_rdoc.html].
The Controller layer is responsible for handling incoming HTTP requests and providing a
suitable response. Usually this means returning HTML, but Rails controllers can also
generate XML, JSON, PDFs, mobile-specific views, and more. Controllers manipulate models
and render view templates in order to generate the appropriate HTTP response.
In Rails, the Controller and View layers are handled together by Action Pack.
These two layers are bundled in a single package due to their heavy interdependence.
This is unlike the relationship between the Active Record and Action Pack which are
independent. Each of these packages can be used independently outside of Rails. You
can read more about Action Pack in its {README}[link:files/actionpack/README_rdoc.html].
== Getting Started
@ -60,9 +65,10 @@ Rails. You can read more about Action Pack in its
== Contributing
We encourage you to contribute to Ruby on Rails! Please check out the {Contributing to Rails
guide}[http://edgeguides.rubyonrails.org/contributing_to_rails.html] for guidelines about how
guide}[http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html] for guidelines about how
to proceed. {Join us}[http://contributors.rubyonrails.org]!
== License
Ruby on Rails is released under the MIT license.

View File

@ -153,7 +153,7 @@ Action Mailer is released under the MIT license.
API documentation is at
* http://api.rubyonrails.com
* http://api.rubyonrails.org
Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:

View File

@ -334,7 +334,7 @@ Action Pack is released under the MIT license.
API documentation is at
* http://api.rubyonrails.com
* http://api.rubyonrails.org
Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:

View File

@ -1,12 +1,12 @@
module ActionController
# This module provides a method which will redirects browser to use HTTPS
# This module provides a method which will redirect browser to use HTTPS
# protocol. This will ensure that user's sensitive information will be
# transferred safely over the internet. You _should_ always force browser
# to use HTTPS when you're transferring sensitive information such as
# user authentication, account information, or credit card information.
#
# Note that if you really concern about your application safety, you might
# consider using +config.force_ssl+ in your configuration config file instead.
# Note that if you are really concerned about your application security,
# you might consider using +config.force_ssl+ in your config file instead.
# That will ensure all the data transferred via HTTPS protocol and prevent
# user from getting session hijacked when accessing the site under unsecured
# HTTP protocol.

View File

@ -578,8 +578,8 @@ module ActionDispatch
# end
#
# This generates helpers such as +account_projects_path+, just like +resources+ does.
# The difference here being that the routes generated are like /rails/projects/2,
# rather than /accounts/rails/projects/2.
# The difference here being that the routes generated are like /:account_id/projects,
# rather than /accounts/:account_id/projects.
#
# === Options
#

View File

@ -215,7 +215,7 @@ Active Record is released under the MIT license.
API documentation is at
* http://api.rubyonrails.com
* http://api.rubyonrails.org
Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:

View File

@ -666,7 +666,7 @@ module ActiveRecord
#
# Consider the following loop using the class above:
#
# for post in Post.all
# Post.all.each do |post|
# puts "Post: " + post.title
# puts "Written by: " + post.author.name
# puts "Last comment on: " + post.comments.first.created_on
@ -675,7 +675,7 @@ module ActiveRecord
# To iterate over these one hundred posts, we'll generate 201 database queries. Let's
# first just optimize it for retrieving the author:
#
# for post in Post.find(:all, :include => :author)
# Post.find(:all, :include => :author).each do |post|
#
# This references the name of the +belongs_to+ association that also used the <tt>:author</tt>
# symbol. After loading the posts, find will collect the +author_id+ from each one and load
@ -684,7 +684,7 @@ module ActiveRecord
#
# We can improve upon the situation further by referencing both associations in the finder with:
#
# for post in Post.find(:all, :include => [ :author, :comments ])
# Post.find(:all, :include => [ :author, :comments ]).each do |post|
#
# This will load all comments with a single query. This reduces the total number of queries
# to 3. More generally the number of queries will be 1 plus the number of associations
@ -692,7 +692,7 @@ module ActiveRecord
#
# To include a deep hierarchy of associations, use a hash:
#
# for post in Post.find(:all, :include => [ :author, { :comments => { :author => :gravatar } } ])
# Post.find(:all, :include => [ :author, { :comments => { :author => :gravatar } } ]).each do |post|
#
# That'll grab not only all the comments but all their authors and gravatar pictures.
# You can mix and match symbols, arrays and hashes in any combination to describe the
@ -1078,7 +1078,8 @@ module ActiveRecord
# alongside this object by calling their +destroy+ method. If set to <tt>:delete_all</tt> all associated
# objects are deleted *without* calling their +destroy+ method. If set to <tt>:nullify</tt> all associated
# objects' foreign keys are set to +NULL+ *without* calling their +save+ callbacks. If set to
# <tt>:restrict</tt> this object cannot be deleted if it has any associated object.
# <tt>:restrict</tt> this object raises an <tt>ActiveRecord::DeleteRestrictionError</tt> exception and
# cannot be deleted if it has any associated objects.
#
# If using with the <tt>:through</tt> option, the association on the join model must be
# a +belongs_to+, and the records which get deleted are the join records, rather than
@ -1227,7 +1228,8 @@ module ActiveRecord
# If set to <tt>:destroy</tt>, the associated object is destroyed when this object is. If set to
# <tt>:delete</tt>, the associated object is deleted *without* calling its destroy method.
# If set to <tt>:nullify</tt>, the associated object's foreign key is set to +NULL+.
# Also, association is assigned.
# Also, association is assigned. If set to <tt>:restrict</tt> this object raises an
# <tt>ActiveRecord::DeleteRestrictionError</tt> exception and cannot be deleted if it has any associated object.
# [:foreign_key]
# Specify the foreign key used for the association. By default this is guessed to be the name
# of this class in lower-case and "_id" suffixed. So a Person class that makes a +has_one+ association

View File

@ -14,7 +14,7 @@ module ActiveRecord
# Account.transaction do
# # select * from accounts where name = 'shugo' limit 1 for update
# shugo = Account.where("name = 'shugo'").lock(true).first
# yuko = Account.where("name = 'shugo'").lock(true).first
# yuko = Account.where("name = 'yuko'").lock(true).first
# shugo.balance -= 100
# shugo.save!
# yuko.balance += 100

View File

@ -137,6 +137,8 @@ module ActiveRecord
# * Callbacks are skipped.
# * updated_at/updated_on column is not updated if that column is available.
#
# Raises an +ActiveRecordError+ when called on new objects, or when the +name+
# attribute is marked as readonly.
def update_column(name, value)
name = name.to_s
raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)

View File

@ -20,6 +20,16 @@ Model classes are mapped to remote REST resources by Active Resource much the sa
tables. When a request is made to a remote resource, a REST XML request is generated, transmitted, and the result
received and serialized into a usable Ruby object.
== Download and installation
The latest version of Active Support can be installed with Rubygems:
% [sudo] gem install activeresource
Source code can be downloaded as part of the Rails project on GitHub
* https://github.com/rails/rails/tree/master/activeresource/
=== Configuration and Usage
Putting Active Resource to use is very similar to Active Record. It's as simple as creating a model class
@ -160,6 +170,18 @@ Destruction of a resource can be invoked as a class and instance method of the r
Person.delete(2) # => true
Person.exists?(2) # => false
== License
Active Support is released under the MIT license.
== Support
API documentation is at
* http://api.rubyonrails.org
Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:
* https://github.com/rails/rails/issues
You can find more usage information in the ActiveResource::Base documentation.

View File

@ -26,7 +26,7 @@ Active Support is released under the MIT license.
API documentation is at
* http://api.rubyonrails.com
* http://api.rubyonrails.org
Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:

View File

@ -419,7 +419,7 @@ class UserMailer < ActionMailer::Base
)
if email.has_attachments?
for attachment in email.attachments
email.attachments.each do |attachment|
page.attachments.create({
:file => attachment,
:description => email.subject

View File

@ -615,7 +615,7 @@ atom_feed do |feed|
feed.title("Posts Index")
feed.updated((@posts.first.created_at))
for post in @posts
@posts.each do |post|
feed.entry(post) do |entry|
entry.title(post.title)
entry.content(post.body, :type => 'html')

View File

@ -165,23 +165,23 @@ Each helper accepts an arbitrary number of attribute names, so with a single lin
All of them accept the +:on+ and +:message+ options, which define when the validation should be run and what message should be added to the +errors+ collection if it fails, respectively. The +:on+ option takes one of the values +:save+ (the default), +:create+ or +:update+. There is a default error message for each one of the validation helpers. These messages are used when the +:message+ option isn't specified. Let's take a look at each one of the available helpers.
h4. +validates_acceptance_of+
h4. +acceptance+
Validates that a checkbox on the user interface was checked when a form was submitted. This is typically used when the user needs to agree to your application's terms of service, confirm reading some text, or any similar concept. This validation is very specific to web applications and this 'acceptance' does not need to be recorded anywhere in your database (if you don't have a field for it, the helper will just create a virtual attribute).
<ruby>
class Person < ActiveRecord::Base
validates_acceptance_of :terms_of_service
validates :terms_of_service, :acceptance => true
end
</ruby>
The default error message for +validates_acceptance_of+ is "_must be accepted_".
The default error message for this helper is "_must be accepted_".
+validates_acceptance_of+ can receive an +:accept+ option, which determines the value that will be considered acceptance. It defaults to "1", but you can change this.
It can receive an +:accept+ option, which determines the value that will be considered acceptance. It defaults to "1" and can be easily changed.
<ruby>
class Person < ActiveRecord::Base
validates_acceptance_of :terms_of_service, :accept => 'yes'
validates :terms_of_service, :acceptance => true, :accept => 'yes'
end
</ruby>
@ -202,13 +202,13 @@ CAUTION: Don't use +validates_associated+ on both ends of your associations. The
The default error message for +validates_associated+ is "_is invalid_". Note that each associated object will contain its own +errors+ collection; errors do not bubble up to the calling model.
h4. +validates_confirmation_of+
h4. +confirmation+
You should use this helper when you have two text fields that should receive exactly the same content. For example, you may want to confirm an email address or a password. This validation creates a virtual attribute whose name is the name of the field that has to be confirmed with "_confirmation" appended.
<ruby>
class Person < ActiveRecord::Base
validates_confirmation_of :email
validates :email, :confirmation => true
end
</ruby>
@ -219,70 +219,70 @@ In your view template you could use something like
<%= text_field :person, :email_confirmation %>
</erb>
This check is performed only if +email_confirmation+ is not +nil+. To require confirmation, make sure to add a presence check for the confirmation attribute (we'll take a look at +validates_presence_of+ later on this guide):
This check is performed only if +email_confirmation+ is not +nil+. To require confirmation, make sure to add a presence check for the confirmation attribute (we'll take a look at +presence+ later on this guide):
<ruby>
class Person < ActiveRecord::Base
validates_confirmation_of :email
validates_presence_of :email_confirmation
validates :email, :confirmation => true
validates :email_confirmation, :presence => true
end
</ruby>
The default error message for +validates_confirmation_of+ is "_doesn't match confirmation_".
The default error message for this helper is "_doesn't match confirmation_".
h4. +validates_exclusion_of+
h4. +exclusion+
This helper validates that the attributes' values are not included in a given set. In fact, this set can be any enumerable object.
<ruby>
class Account < ActiveRecord::Base
validates_exclusion_of :subdomain, :in => %w(www us ca jp),
:message => "Subdomain %{value} is reserved."
validates :subdomain, :exclusion => { :in => %w(www us ca jp),
:message => "Subdomain %{value} is reserved." }
end
</ruby>
The +validates_exclusion_of+ helper has an option +:in+ that receives the set of values that will not be accepted for the validated attributes. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. This example uses the +:message+ option to show how you can include the attribute's value.
The +exclusion+ helper has an option +:in+ that receives the set of values that will not be accepted for the validated attributes. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. This example uses the +:message+ option to show how you can include the attribute's value.
The default error message for +validates_exclusion_of+ is "_is reserved_".
The default error message is "_is reserved_".
h4. +validates_format_of+
h4. +format+
This helper validates the attributes' values by testing whether they match a given regular expression, which is specified using the +:with+ option.
<ruby>
class Product < ActiveRecord::Base
validates_format_of :legacy_code, :with => /\A[a-zA-Z]+\z/,
:message => "Only letters allowed"
validates :legacy_code, :format => { :with => /\A[a-zA-Z]+\z/,
:message => "Only letters allowed" }
end
</ruby>
The default error message for +validates_format_of+ is "_is invalid_".
The default error message is "_is invalid_".
h4. +validates_inclusion_of+
h4. +inclusion+
This helper validates that the attributes' values are included in a given set. In fact, this set can be any enumerable object.
<ruby>
class Coffee < ActiveRecord::Base
validates_inclusion_of :size, :in => %w(small medium large),
:message => "%{value} is not a valid size"
validates :size, :inclusion => { :in => %w(small medium large),
:message => "%{value} is not a valid size" }
end
</ruby>
The +validates_inclusion_of+ helper has an option +:in+ that receives the set of values that will be accepted. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. The previous example uses the +:message+ option to show how you can include the attribute's value.
The +inclusion+ helper has an option +:in+ that receives the set of values that will be accepted. The +:in+ option has an alias called +:within+ that you can use for the same purpose, if you'd like to. The previous example uses the +:message+ option to show how you can include the attribute's value.
The default error message for +validates_inclusion_of+ is "_is not included in the list_".
The default error message for this helper is "_is not included in the list_".
h4. +validates_length_of+
h4. +length+
This helper validates the length of the attributes' values. It provides a variety of options, so you can specify length constraints in different ways:
<ruby>
class Person < ActiveRecord::Base
validates_length_of :name, :minimum => 2
validates_length_of :bio, :maximum => 500
validates_length_of :password, :in => 6..20
validates_length_of :registration_number, :is => 6
validates :name, :length => { :minimum => 2 }
validates :bio, :length => { :maximum => 500 }
validates :password, :length => { :in => 6..20 }
validates :registration_number, :length => { :is => 6 }
end
</ruby>
@ -297,8 +297,8 @@ The default error messages depend on the type of length validation being perform
<ruby>
class Person < ActiveRecord::Base
validates_length_of :bio, :maximum => 1000,
:too_long => "%{count} characters is the maximum allowed"
validates :bio, :length => { :maximum => 1000,
:too_long => "%{count} characters is the maximum allowed" }
end
</ruby>
@ -306,20 +306,21 @@ This helper counts characters by default, but you can split the value in a diffe
<ruby>
class Essay < ActiveRecord::Base
validates_length_of :content,
validates :content, :length => {
:minimum => 300,
:maximum => 400,
:tokenizer => lambda { |str| str.scan(/\w+/) },
:too_short => "must have at least %{count} words",
:too_long => "must have at most %{count} words"
}
end
</ruby>
Note that the default error messages are plural (e.g., "is too short (minimum is %{count} characters)"). For this reason, when +:minimum+ is 1 you should provide a personalized message or use +validates_presence_of+ instead. When +:in+ or +:within+ have a lower limit of 1, you should either provide a personalized message or call +validates_presence_of+ prior to +validates_length_of+.
Note that the default error messages are plural (e.g., "is too short (minimum is %{count} characters)"). For this reason, when +:minimum+ is 1 you should provide a personalized message or use +validates_presence_of+ instead. When +:in+ or +:within+ have a lower limit of 1, you should either provide a personalized message or call +presence+ prior to +length+.
The +validates_size_of+ helper is an alias for +validates_length_of+.
The +size+ helper is an alias for +length+.
h4. +validates_numericality_of+
h4. +numericality+
This helper validates that your attributes have only numeric values. By default, it will match an optional sign followed by an integral or floating point number. To specify that only integral numbers are allowed set +:only_integer+ to true.
@ -335,12 +336,12 @@ WARNING. Note that the regular expression above allows a trailing newline charac
<ruby>
class Player < ActiveRecord::Base
validates_numericality_of :points
validates_numericality_of :games_played, :only_integer => true
validates :points, :numericality => true
validates :games_played, :numericality => true, :only_integer => true
end
</ruby>
Besides +:only_integer+, the +validates_numericality_of+ helper also accepts the following options to add constraints to acceptable values:
Besides +:only_integer+, this helper also accepts the following options to add constraints to acceptable values:
* +:greater_than+ - Specifies the value must be greater than the supplied value. The default error message for this option is "_must be greater than %{count}_".
* +:greater_than_or_equal_to+ - Specifies the value must be greater than or equal to the supplied value. The default error message for this option is "_must be greater than or equal to %{count}_".
@ -350,9 +351,9 @@ Besides +:only_integer+, the +validates_numericality_of+ helper also accepts the
* +:odd+ - Specifies the value must be an odd number if set to true. The default error message for this option is "_must be odd_".
* +:even+ - Specifies the value must be an even number if set to true. The default error message for this option is "_must be even_".
The default error message for +validates_numericality_of+ is "_is not a number_".
The default error message is "_is not a number_".
h4. +validates_presence_of+
h4. +presence+
This helper validates that the specified attributes are not empty. It uses the +blank?+ method to check if the value is either +nil+ or a blank string, that is, a string that is either empty or consists of whitespace.
@ -367,21 +368,21 @@ If you want to be sure that an association is present, you'll need to test wheth
<ruby>
class LineItem < ActiveRecord::Base
belongs_to :order
validates_presence_of :order_id
validates :order_id, :presence => true
end
</ruby>
Since +false.blank?+ is true, if you want to validate the presence of a boolean field you should use +validates_inclusion_of :field_name, :in => [true, false]+.
Since +false.blank?+ is true, if you want to validate the presence of a boolean field you should use <tt>validates :field_name, :inclusion => { :in => [true, false] }</tt>.
The default error message for +validates_presence_of+ is "_can't be empty_".
The default error message is "_can't be empty_".
h4. +validates_uniqueness_of+
h4. +uniqueness+
This helper validates that the attribute's value is unique right before the object gets saved. It does not create a uniqueness constraint in the database, so it may happen that two different database connections create two records with the same value for a column that you intend to be unique. To avoid that, you must create a unique index in your database.
<ruby>
class Account < ActiveRecord::Base
validates_uniqueness_of :email
validates :email, :uniqueness => true
end
</ruby>
@ -391,7 +392,7 @@ There is a +:scope+ option that you can use to specify other attributes that are
<ruby>
class Holiday < ActiveRecord::Base
validates_uniqueness_of :name, :scope => :year,
validates :name, :uniqueness => true, :scope => :year,
:message => "should happen once per year"
end
</ruby>
@ -400,13 +401,13 @@ There is also a +:case_sensitive+ option that you can use to define whether the
<ruby>
class Person < ActiveRecord::Base
validates_uniqueness_of :name, :case_sensitive => false
validates :name, :uniqueness => true, :case_sensitive => false
end
</ruby>
WARNING. Note that some databases are configured to perform case-insensitive searches anyway.
The default error message for +validates_uniqueness_of+ is "_has already been taken_".
The default error message is "_has already been taken_".
h4. +validates_with+
@ -470,8 +471,8 @@ The +:allow_nil+ option skips the validation when the value being validated is +
<ruby>
class Coffee < ActiveRecord::Base
validates_inclusion_of :size, :in => %w(small medium large),
:message => "%{value} is not a valid size", :allow_nil => true
validates :size, :inclusion => { :in => %w(small medium large),
:message => "%{value} is not a valid size" }, :allow_nil => true
end
</ruby>
@ -483,7 +484,7 @@ The +:allow_blank+ option is similar to the +:allow_nil+ option. This option wil
<ruby>
class Topic < ActiveRecord::Base
validates_length_of :title, :is => 5, :allow_blank => true
validates :title, :length => { :is => 5 }, :allow_blank => true
end
Topic.create("title" => "").valid? # => true
@ -503,10 +504,10 @@ The +:on+ option lets you specify when the validation should happen. The default
<ruby>
class Person < ActiveRecord::Base
# it will be possible to update email with a duplicated value
validates_uniqueness_of :email, :on => :create
validates :email, :uniqueness => true, :on => :create
# it will be possible to create the record with a non-numerical age
validates_numericality_of :age, :on => :update
validates :age, :numericality => true, :on => :update
# the default (validates on both create and update)
validates :name, :presence => true, :on => :save
@ -523,7 +524,7 @@ You can associate the +:if+ and +:unless+ options with a symbol corresponding to
<ruby>
class Order < ActiveRecord::Base
validates_presence_of :card_number, :if => :paid_with_card?
validates :card_number, :presence => true, :if => :paid_with_card?
def paid_with_card?
payment_type == "card"
@ -537,7 +538,7 @@ You can also use a string that will be evaluated using +eval+ and needs to conta
<ruby>
class Person < ActiveRecord::Base
validates_presence_of :surname, :if => "name.nil?"
validates :surname, :presence => true, :if => "name.nil?"
end
</ruby>
@ -547,7 +548,7 @@ Finally, it's possible to associate +:if+ and +:unless+ with a +Proc+ object whi
<ruby>
class Account < ActiveRecord::Base
validates_confirmation_of :password,
validates :password, :confirmation => true,
:unless => Proc.new { |a| a.password.blank? }
end
</ruby>
@ -559,8 +560,8 @@ Sometimes it is useful to have multiple validations use one condition, it can be
<ruby>
class User < ActiveRecord::Base
with_options :if => :is_admin? do |admin|
admin.validates_length_of :password, :minimum => 10
admin.validates_presence_of :email
admin.validates :password, :length => { :minimum => 10 }
admin.validates :email, :presence => true
end
end
</ruby>
@ -597,7 +598,7 @@ You can even create your own validation helpers and reuse them in several differ
<ruby>
ActiveRecord::Base.class_eval do
def self.validates_as_choice(attr_name, n, options={})
validates_inclusion_of attr_name, {:in => 1..n}.merge(options)
validates attr_name, :inclusion => { {:in => 1..n}.merge(options) }
end
end
</ruby>
@ -622,8 +623,7 @@ Returns an OrderedHash with all errors. Each key is the attribute name and the v
<ruby>
class Person < ActiveRecord::Base
validates :name, :presence => true
validates_length_of :name, :minimum => 3
validates :name, :presence => true, :length => { :minimum => 3 }
end
person = Person.new
@ -642,8 +642,7 @@ h4(#working_with_validation_errors-errors-2). +errors[]+
<ruby>
class Person < ActiveRecord::Base
validates :name, :presence => true
validates_length_of :name, :minimum => 3
validates :name, :presence => true, :length => { :minimum => 3 }
end
person = Person.new(:name => "John Doe")
@ -718,8 +717,7 @@ The +clear+ method is used when you intentionally want to clear all the messages
<ruby>
class Person < ActiveRecord::Base
validates :name, :presence => true
validates_length_of :name, :minimum => 3
validates :name, :presence => true, :length => { :minimum => 3 }
end
person = Person.new
@ -742,9 +740,7 @@ The +size+ method returns the total number of error messages for the object.
<ruby>
class Person < ActiveRecord::Base
validates :name, :presence => true
validates_length_of :name, :minimum => 3
validates_presence_of :email
validates :name, :presence => true, :length => { :minimum => 3 }
end
person = Person.new
@ -765,13 +761,14 @@ h4. Installing as a plugin
$ rails plugin install git://github.com/joelmoss/dynamic_form.git
</shell>
h4 Installing as a Gem
Add this line on your Gemfile:
h4. Installing as a Gem
Add this line in your Gemfile:
<ruby>
gem "dynamic_form"
</ruby>
Now you will have access to these two methods in your view templates:
Now you will have access to these two methods in your view templates.
h4. +error_messages+ and +error_messages_for+
@ -779,8 +776,8 @@ When creating a form with the +form_for+ helper, you can use the +error_messages
<ruby>
class Product < ActiveRecord::Base
validates_presence_of :description, :value
validates_numericality_of :value, :allow_nil => true
validates :description, :value, :presence => true
validates :value, :numericality => true, :allow_nil => true
end
</ruby>
@ -878,7 +875,7 @@ In order to use the available callbacks, you need to register them. You can do t
<ruby>
class User < ActiveRecord::Base
validates_presence_of :login, :email
validates :login, :email, :presence => true
before_validation :ensure_login_has_a_value
@ -895,7 +892,7 @@ The macro-style class methods can also receive a block. Consider using this styl
<ruby>
class User < ActiveRecord::Base
validates_presence_of :login, :email
validates :login, :email, :presence => true
before_create do |user|
user.name = user.login.capitalize if user.name.blank?

View File

@ -436,20 +436,6 @@ end
NOTE: Defined in +active_support/core_ext/kernel/reporting.rb+.
h4. +require_library_or_gem+
The convenience method +require_library_or_gem+ tries to load its argument with a regular +require+ first. If it fails loads +rubygems+ and tries again.
If the first attempt is a failure and +rubygems+ can't be loaded the method raises +LoadError+. A +LoadError+ is also raised if +rubygems+ is available but the argument is not loadable as a gem.
For example, that's the way the MySQL adapter loads the MySQL library:
<ruby>
require_library_or_gem('mysql')
</ruby>
NOTE: Defined in +active_support/core_ext/kernel/requires.rb+.
h4. +in?+
The predicate +in?+ tests if an object is included in another object. An +ArgumentError+ exception will be raised if the argument passed does not respond to +include?+.

View File

@ -0,0 +1,29 @@
h2. Asset Pipeline
This guide will cover the ideology of the asset pipeline introduced in Rails 3.1.
By referring to this guide you will be able to:
* Properly organize your application assets
* Understand the benefits of the asset pipline
* Adding a preproccessor to the pipeline
* Package assets with your plugin
endprologue.
h3. What Is The Asset Pipeline?
h4. Why Should I Use it?
h3. How to Use the Asset Pipeline
h4. Asset Organization
h4. Default Files Loaded
h4. Directives
h4. Stacking Preproccessors
h3. Packaging Assets with Your Plugin
h3. More on Sprockets

View File

@ -414,7 +414,7 @@ Rails has 5 initialization events which can be hooked into (listed in order that
* +to_prepare+: Run after the initializers are ran for all Railties (including the application itself), but before eager loading and the middleware stack is built.
* +before_eager_load+: This is run directly before eager loading occurs, which is the default behaviour for the _production_ environment and not for the +development+ enviroment.
* +before_eager_load+: This is run directly before eager loading occurs, which is the default behaviour for the _production_ environment and not for the +development+ environment.
* +after_initialize+: Run directly after the initialization of the application, but before the application initializers are run.

View File

@ -712,7 +712,7 @@ You might want to render a form with a set of edit fields for each of a person's
<erb>
<%= form_for @person do |person_form| %>
<%= person_form.text_field :name %>
<% for address in @person.addresses %>
<% @person.addresses.each do |address| %>
<%= person_form.fields_for address, :index => address do |address_form|%>
<%= address_form.text_field :city %>
<% end %>

View File

@ -25,33 +25,36 @@ endprologue.
h3. Setup
h4. Generating the Plugin Skeleton
Before you continue, take a moment to decide if your new plugin will be potentially shared across different Rails applications.
Rails currently ships with a generator to generate a plugin within a Rails application. Help text is available that will explain
how this generator works.
* If your plugin is specific to your application, your new plugin will be a _vendored plugin_.
* If you think your plugin may be used across applications, build it as a _gemified plugin_.
h4. Either generate a vendored plugin...
Use the +rails generate plugin+ command in your Rails root directory
to create a new plugin that will live in the +vendor/plugins+
directory. See usage and options by asking for help:
<shell>
$ rails generate plugin --help
$ rails generate plugin new --help
</shell>
This generator places the plugin into the vendor/plugins directory.
h4. Or generate a gemified plugin.
Vendored plugins are useful for quickly prototyping your plugin but current thinking in the Rails community is shifting towards
packaging plugins as gems, especially with the inclusion of Bundler as the Rails dependency manager.
Packaging a plugin as a gem may be overkill for any plugins that will not be shared across projects but doing so from the start makes it easier to share the plugin going forward without adding too much additional overhead during development.
Writing your Rails plugin as a gem, rather than as a vendored plugin,
lets you share your plugin across different rails applications using
RubyGems and Bundler.
Rails 3.1 will ship with a plugin generator that will default to setting up a plugin
as a gem. This tutorial will begin to bridge that gap by demonstrating how to create a gem based plugin using the
"Enginex gem":http://www.github.com/josevalim/enginex.
Rails 3.1 ships with a +rails plugin new+ command which creates a
skeleton for developing any kind of Rails extension with the ability
to run integration tests using a dummy Rails application. See usage
and options by asking for help:
<shell>
$ gem install enginex
$ enginex --help
$ enginex yaffle
$ rails plugin --help
</shell>
This command will create a new directory named "yaffle" within the current directory.
h3. Testing your newly generated plugin
You can navigate to the directory that contains the plugin, run the +bundle install+ command
@ -387,9 +390,7 @@ Run +rake+ one final time and you should see:
7 tests, 7 assertions, 0 failures, 0 errors, 0 skips
</shell>
NOTE: The use of +write_attribute+ to write to the field in model is just one example of how a plugin can
interact with the model, and will not always be the right method to use. For example, you could also
use +send("#{self.class.yaffle_text_field}=", string.to_squawk)+.
NOTE: The use of +write_attribute+ to write to the field in model is just one example of how a plugin can interact with the model, and will not always be the right method to use. For example, you could also use <tt>send("#{self.class.yaffle_text_field}=", string.to_squawk)</tt>.
h3. Generators

View File

@ -880,7 +880,7 @@ h3. Changelog
* April 10, 2010: Updated guide to remove outdated and superfluous information, and to provide information about new features, by "Yehuda Katz":http://www.yehudakatz.com
* April 2, 2010: Updated guide to match new Routing DSL in Rails 3, by "Rizwan Reza":http://www.rizwanreza.com/
* Febuary 1, 2010: Modifies the routing documentation to match new routing DSL in Rails 3, by Prem Sichanugrist
* February 1, 2010: Modifies the routing documentation to match new routing DSL in Rails 3, by Prem Sichanugrist
* October 4, 2008: Added additional detail on specifying verbs for resource member/collection routes, by "Mike Gunderloy":credits.html#mgunderloy
* September 23, 2008: Added section on namespaced controllers and routing, by "Mike Gunderloy":credits.html#mgunderloy
* September 10, 2008: initial version by "Mike Gunderloy":credits.html#mgunderloy

View File

@ -62,10 +62,10 @@ To force process of all the guides, pass +ALL=1+.
It is also recommended that you work with +WARNINGS=1+. This detects duplicate IDs and warns about broken internal links.
If you want to generate guides in languages other than English, you can keep them in a separate directory under +source+ (eg. <tt>source/es</tt>) and use the +LANGUAGE+ environment variable:
If you want to generate guides in languages other than English, you can keep them in a separate directory under +source+ (eg. <tt>source/es</tt>) and use the +GUIDES_LANGUAGE+ environment variable:
<plain>
rake generate_guides LANGUAGE=es
rake generate_guides GUIDES_LANGUAGE=es
</plain>
h3. HTML Validation

View File

@ -540,7 +540,7 @@ Most bots are really dumb, they crawl the web and put their spam into every form
Here are some ideas how to hide honeypot fields by JavaScript and/or CSS:
* position the fields off of the visible area of the page
* make the elements very small or colour them the same as the background of the page
* make the elements very small or color them the same as the background of the page
* leave the fields displayed, but tell humans to leave them blank
The most simple negative CAPTCHA is one hidden honeypot field. On the server side, you will check the value of the field: If it contains any text, it must be a bot. Then, you can either ignore the post or return a positive result, but not saving the post to the database. This way the bot will be satisfied and moves on. You can do this with annoying users, too.
@ -567,7 +567,7 @@ h4. Good Passwords
-- _Do you find it hard to remember all your passwords? Don't write them down, but use the initial letters of each word in an easy to remember sentence._
Bruce Schneier, a security technologist, "has analysed":http://www.schneier.com/blog/archives/2006/12/realworld_passw.html 34,000 real-world user names and passwords from the MySpace phishing attack mentioned <a href="#examples-from-the-underground">below</a>. It turns out that most of the passwords are quite easy to crack. The 20 most common passwords are:
Bruce Schneier, a security technologist, "has analyzed":http://www.schneier.com/blog/archives/2006/12/realworld_passw.html 34,000 real-world user names and passwords from the MySpace phishing attack mentioned <a href="#examples-from-the-underground">below</a>. It turns out that most of the passwords are quite easy to crack. The 20 most common passwords are:
password1, abc123, myspace1, password, blink182, qwerty1, ****you, 123abc, baseball1, football1, 123456, soccer, monkey1, liverpool1, princess1, jordan23, slipknot1, superman1, iloveyou1, and monkey.
@ -885,7 +885,7 @@ The "moz-binding":http://www.securiteam.com/securitynews/5LP051FHPE.html CSS pro
h5(#css-injection-countermeasures). Countermeasures
This example, again, showed that a blacklist filter is never complete. However, as custom CSS in web applications is a quite rare feature, I am not aware of a whitelist CSS filter. _(highlight)If you want to allow custom colours or images, you can allow the user to choose them and build the CSS in the web application_. Use Rails' +sanitize()+ method as a model for a whitelist CSS filter, if you really need one.
This example, again, showed that a blacklist filter is never complete. However, as custom CSS in web applications is a quite rare feature, I am not aware of a whitelist CSS filter. _(highlight)If you want to allow custom colors or images, you can allow the user to choose them and build the CSS in the web application_. Use Rails' +sanitize()+ method as a model for a whitelist CSS filter, if you really need one.
h4. Textile Injection

View File

@ -180,7 +180,7 @@ module Rails
# <tt>my_engine:install:assets</tt>
#
# Engine name is set by default based on class name. For <tt>MyEngine::Engine</tt> it will be
# <tt>my_engine_engine</tt>. You can change it manually it manually using the <tt>engine_name</tt> method:
# <tt>my_engine_engine</tt>. You can change it manually using the <tt>engine_name</tt> method:
#
# module MyEngine
# class Engine < Rails::Engine
@ -296,7 +296,7 @@ module Rails
# helper MyEngine::SharedEngineHelper
# end
#
# If you want to include all of the engine's helpers, you can use #helpers method on egine's
# If you want to include all of the engine's helpers, you can use #helpers method on an engine's
# instance:
#
# class ApplicationController < ActionController::Base
@ -305,7 +305,7 @@ module Rails
#
# It will include all of the helpers from engine's directory. Take into account that this does
# not include helpers defined in controllers with helper_method or other similar solutions,
# only helpers defined in helpers directory will be included.
# only helpers defined in the helpers directory will be included.
#
# == Migrations & seed data
#