Skip to content

Commit

Permalink
Merge pull request rails#6292 from erichmenge/3-2-stable-docfix
Browse files Browse the repository at this point in the history
3 2 stable guides fix
  • Loading branch information
rafaelfranca committed May 12, 2012
2 parents 9cead4a + ccf80c2 commit 41e7a2a
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion railties/guides/source/getting_started.textile
Expand Up @@ -685,14 +685,17 @@ The model file, +app/models/post.rb+ is about as simple as it can get:

<ruby>
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title
end
</ruby>

There isn't much to this file - but note that the +Post+ class inherits from
+ActiveRecord::Base+. Active Record supplies a great deal of functionality to
your Rails models for free, including basic database CRUD (Create, Read, Update,
Destroy) operations, data validation, as well as sophisticated search support
and the ability to relate multiple models to one another.
and the ability to relate multiple models to one another. Another important part
of this file is +attr_accessible+. It specifies a whitelist of attributes that are
allowed to be updated in bulk (via +update_attributes+ for instance).

h4. Adding Some Validation

Expand All @@ -701,6 +704,8 @@ Open the +app/models/post.rb+ file and edit it:

<ruby>
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title

validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
Expand Down Expand Up @@ -1218,6 +1223,8 @@ You'll need to edit the +post.rb+ file to add the other side of the association:

<ruby>
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title

validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
Expand Down Expand Up @@ -1605,6 +1612,8 @@ model, +app/models/post.rb+, as follows:

<ruby>
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title

validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
Expand Down Expand Up @@ -1686,6 +1695,8 @@ edit tags via posts:

<ruby>
class Post < ActiveRecord::Base
attr_accessible :content, :name, :title, :tags_attributes

validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
Expand All @@ -1703,6 +1714,10 @@ nested attributes (you'll handle that by displaying a "remove" checkbox on the
view that you'll build shortly). The +:reject_if+ option prevents saving new
tags that do not have any attributes filled in.

Also note we had to add +:tags_attributes+ to the +attr_accessible+ list. If
we didn't do this there would be a +MassAssignmentSecurity+ exception when we try to
update tags through our posts model.

We will modify +views/posts/_form.html.erb+ to render a partial to make a tag:

<erb>
Expand Down

0 comments on commit 41e7a2a

Please sign in to comment.