<divclass="para"><p>The new bang! version of <tt>find_by_<attribute>! is equivalent to +Model.first(:conditions ⇒ {:attribute ⇒ value}) || raise ActiveRecord::RecordNotFound</tt> Instead of returning <tt>nil</tt> if it can't find a matching record, this method will raise an exception if it cannot find a match.</p></div>
<divclass="para"><p>The new bang! version of <tt>find_by_<attribute>!</tt> is equivalent to <tt>Model.first(:conditions ⇒ {:attribute ⇒ value}) || raise ActiveRecord::RecordNotFound</tt> Instead of returning <tt>nil</tt> if it can't find a matching record, this method will raise an exception if it cannot find a match.</p></div>
<divclass="listingblock">
<divclass="content"><!-- Generator: GNU source-highlight 2.9
by Lorenzo Bettini
...
...
@@ -779,6 +779,16 @@ Benchmarking numbers are now reported in milliseconds rather than tiny fractions
Rails now supports HTTP-only cookies (and uses them for sessions), which help mitigate some cross-site scripting risks in newer browsers.
</p>
</li>
<li>
<p>
<tt>redirect_to</tt> now fully supports URI schemes (so, for example, you can redirect to a svn+ssh: URI).
</p>
</li>
<li>
<p>
<tt>render</tt> now supports a <tt>:js</tt> option to render plain vanilla javascript with the right mime type.
</p>
</li>
</ul></div>
</div>
<h2id="_action_view">7. Action View</h2>
...
...
@@ -791,7 +801,7 @@ Rails now supports HTTP-only cookies (and uses them for sessions), which help mi
</li>
<li>
<p>
The included Prototype javascript library has been upgraded to version 1.6.0.2.
The included Prototype javascript library has been upgraded to version 1.6.0.3.
<divclass="para"><p>This will produce delegated methods <tt>vendor.account_email</tt> and <tt>vendor.account_password</tt>. You can also specify a custom prefix:</p></div>
<divclass="para"><p>This will produce delegated methods <tt>vendor#account_email</tt> and <tt>vendor#account_password</tt>. You can also specify a custom prefix:</p></div>
<divclass="listingblock">
<divclass="content"><!-- Generator: GNU source-highlight 2.9
<h3id="_other_active_support_changes">9.4. Other Active Support Changes</h3>
<divclass="ilist"><ul>
...
...
@@ -953,6 +963,11 @@ The addition of <tt>ActiveSupport::Rescuable</tt> allows any class to mix in the
The included TzInfo library has been upgraded to version 0.3.11.
</p>
</li>
<li>
<p>
<tt>ActiveSuport::StringInquirer</tt> gives you a pretty way to test for equality in strings: <tt>ActiveSupport::StringInquirer.new("abc").abc? ⇒ true</tt>
</p>
</li>
</ul></div>
</div>
<h2id="_railties">10. Railties</h2>
...
...
@@ -997,7 +1012,7 @@ The included TzInfo library has been upgraded to version 0.3.11.
</p>
</li>
</ul></div>
<divclass="para"><p>You can unpack or install a single gem by specifying <tt>GEM=_gem_name</tt> on the command line.</p></div>
<divclass="para"><p>You can unpack or install a single gem by specifying <tt>GEM=<em>gem_name</em></tt> on the command line.</p></div>
<divclass="ilist"><ul>
<li>
<p>
...
...
@@ -1014,6 +1029,11 @@ More information:
<ahref="http://ryandaigle.com/articles/2008/4/1/what-s-new-in-edge-rails-gem-dependencies">What's New in Edge Rails: Gem Dependencies</a>
</p>
</li>
<li>
<p>
<ahref="http://afreshcup.com/2008/10/25/rails-212-and-22rc1-update-your-rubygems/">Rails 2.1.2 and 2.2RC1: Update Your RubyGems</a>
</p>
</li>
</ul></div>
</li>
</ul></div>
...
...
@@ -1046,7 +1066,7 @@ Instructions for setting up a continuous integration server to build Rails itsel
</li>
<li>
<p>
Wrapped <tt>Rails.env</tt> in <tt>StringQuestioneer</tt> so you can do <tt>Rails.env.development?</tt>
Wrapped <tt>Rails.env</tt> in <tt>StringInquirer</tt> so you can do <tt>Rails.env.development?</tt>
<divclass="para"><p>Now, the LoginsController's "new" and "create" actions will work as before without requiring the user to be logged in. The <tt>:only</tt> option is used to only skip this filter for these actions, and there is also an <tt>:except</tt> option which works the other way. These options can be used when adding filters too, so you can add a filter which only runs for selected actions in the first place.</p></div>
<h3id="_after_filters_and_around_filters">6.1. After filters and around filters</h3>
<divclass="para"><p>In addition to the before filters, you can run filters after an action has run or both before and after. The after filter is similar to the before filter, but because the action has already been run it has access to the response data that's about to be sent to the client. Obviously, after filters can not stop the action from running. Around filters are responsible for running the action, but they can choose not to, which is the around filter's way of stopping it.</p></div>
<divclass="para"><p>TODO: Find a real example for an around filter</p></div>
<divclass="listingblock">
<divclass="content"><!-- Generator: GNU source-highlight 2.9
<divclass="para"><p>If you set the <tt>:readonly</tt> option to <tt>true</tt>, then the associated object will be read-only when retrieved via the association.</p></div>
<h5id="_tt_select_tt"><tt>:select</tt></h5>
<divclass="para"><p>The <tt>:select</tt> option lets you override the SQL <tt>SELECT</tt> clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.</p></div>
<divclass="admonitionblock">
<table><tr>
<tdclass="icon">
<imgsrc="./images/icons/tip.png"alt="Tip"/>
</td>
<tdclass="content">If you set the <tt>:select</tt> option on a <tt>belongs_to</tt> association, you should also set the <tt>foreign_key</tt> option to guarantee the correct results.</td>
</tr></table>
</div>
<h5id="_tt_validate_tt"><tt>:validate</tt></h5>
<divclass="para"><p>If you set the <tt>:validate</tt> option to <tt>true</tt>, then associated objects will be validated whenever you save this object. By default, this is <tt>false</tt>: associated objects will not be validated when this object is saved.</p></div>
<h4id="_when_are_objects_saved">4.1.3. When are Objects Saved?</h4>
@@ -2010,6 +2010,11 @@ The <a href="http://wiki.rubyonrails.org/rails">Rails wiki</a>
<divclass="ilist"><ul>
<li>
<p>
November 1, 2008: First approved version by <ahref="../authors.html#mgunderloy">Mike Gunderloy</a>
</p>
</li>
<li>
<p>
October 16, 2008: Revised based on feedback from Pratik Naik by <ahref="../authors.html#mgunderloy">Mike Gunderloy</a> (not yet approved for publication)
<divclass="para"><p>As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you'll get <tt>admin_photos_url</tt> that expects to find an <tt>Admin::PhotosController</tt> and that matches <tt>admin/photos</tt>, and <tt>admin_photos_ratings+path</tt> that matches <tt>/admin/photos/<em>photo_id</em>/ratings</tt>, expecting to use <tt>Admin::RatingsController</tt>. Even though you're not specifying <tt>path_prefix</tt> explicitly, the routing code will calculate the appropriate <tt>path_prefix</tt> from the route nesting.</p></div>
<divclass="para"><p>As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you'll get <tt>admin_photos_url</tt> that expects to find an <tt>Admin::PhotosController</tt> and that matches <tt>admin/photos</tt>, and <tt>admin_photos_ratings_path</tt> that matches <tt>/admin/photos/<em>photo_id</em>/ratings</tt>, expecting to use <tt>Admin::RatingsController</tt>. Even though you're not specifying <tt>path_prefix</tt> explicitly, the routing code will calculate the appropriate <tt>path_prefix</tt> from the route nesting.</p></div>
<h3id="_adding_more_restful_actions">3.11. Adding More RESTful Actions</h3>
<divclass="para"><p>You are not limited to the seven routes that RESTful routing creates by default. If you like, you may add additional member routes (those which apply to a single instance of the resource), additional new routes (those that apply to creating a new resource), or additional collection routes (those which apply to the collection of resources as a whole).</p></div>
<h4id="_adding_member_routes">3.11.1. Adding Member Routes</h4>
@@ -27,20 +27,20 @@ Along with thread safety, a lot of work has been done to make Rails work well wi
The internal documentation of Rails, in the form of code comments, has been improved in numerous places. In addition, the link:http://guides.rubyonrails.org/[Ruby on Rails Guides] project is the definitive source for information on major Rails components. In its first official release, the Guides page includes:
* Getting Started with Rails
* Rails Database Migrations
* Active Record Associations
* Active Record Finders
* Layouts and Rendering in Rails
* Action View Form Helpers
* Rails Routing from the Outside In
* Basics of Action Controller
* Rails Caching
* Testing Rails Applications
* Securing Rails Applications
* Debugging Rails Applications
* Benchmarking and Profiling Rails Applications
* The Basics of Creating Rails Plugins
* link:http://guides.rubyonrails.org/getting_started_with_rails.html[Getting Started with Rails]
* Lead Contributor: link:http://www.workingwithrails.com/person/9147-emilio-tagua[Emilio Tagua]
==== find_by_<attributes>!
==== +find_by_<attribute>!+
The new bang! version of +find_by_<attribute>! is equivalent to +Model.first(:conditions => {:attribute => value}) || raise ActiveRecord::RecordNotFound+ Instead of returning +nil+ if it can't find a matching record, this method will raise an exception if it cannot find a match.
The new bang! version of +find_by_<attribute>!+ is equivalent to +Model.first(:conditions => {:attribute => value}) || raise ActiveRecord::RecordNotFound+ Instead of returning +nil+ if it can't find a matching record, this method will raise an exception if it cannot find a match.
@@ -257,11 +257,13 @@ Action Controller now offers good support for HTTP conditional GET requests, as
* The HTTP Accept header is disabled by default now. You should prefer the use of formatted URLs (such as +/customers/1.xml+) to indicate the format that you want. If you need the Accept headers, you can turn them back on with +config.action_controller.user_accept_header = true+.
* Benchmarking numbers are now reported in milliseconds rather than tiny fractions of seconds
* Rails now supports HTTP-only cookies (and uses them for sessions), which help mitigate some cross-site scripting risks in newer browsers.
* +redirect_to+ now fully supports URI schemes (so, for example, you can redirect to a svn+ssh: URI).
* +render+ now supports a +:js+ option to render plain vanilla javascript with the right mime type.
== Action View
* +javascript_include_tag+ and +stylesheet_link_tag+ support a new +:recursive+ option to be used along with +:all+, so that you can load an entire tree of files with a single line of code.
* The included Prototype javascript library has been upgraded to version 1.6.0.2.
* The included Prototype javascript library has been upgraded to version 1.6.0.3.
* +RJS#page.reload+ to reload the browser's current location via javascript
* The +atom_feed+ helper now takes an +:instruct+ option to let you insert XML processing instructions.
...
...
@@ -323,23 +325,23 @@ If you delegate behavior from one class to another, you can now specify a prefix
@@ -55,8 +55,6 @@ Now, the LoginsController's "new" and "create" actions will work as before witho
In addition to the before filters, you can run filters after an action has run or both before and after. The after filter is similar to the before filter, but because the action has already been run it has access to the response data that's about to be sent to the client. Obviously, after filters can not stop the action from running. Around filters are responsible for running the action, but they can choose not to, which is the around filter's way of stopping it.
TODO: Find a real example for an around filter
[source, ruby]
---------------------------------
# Example taken from the Rails API filter documentation:
Active Record is a design pattern that mitigates the mind-numbing mental gymnastics often needed to get your application to communicate with a database. This guide explains in detail how This guide uses a mix of real-world examples, metaphors and detailed explanations of the actual Rails source code to help you make the most of AcitveRecord.
Active Record is a design pattern that mitigates the mind-numbing mental gymnastics often needed to get your application to communicate with a database. This guide uses a mix of real-world examples, metaphors and detailed explanations of the actual Rails source code to help you make the most of AcitveRecord.
After reading this guide readers should have a strong grasp of the Active Record pattern and how it can be used with or without Rails. Hopefully, some of the philosophical and theoretical intentions discussed here will also make them a stronger and better developer.
...
...
@@ -178,4 +178,4 @@ Rails has a reputation of being a zero-config framework which means that it aim
With this setup, you can retrieve +@employee.subordinates+ and +@employee.managers+.
With this setup, you can retrieve +@employee.subordinates+ and +@employee.manager+.
== Tips, Tricks, and Warnings
...
...
@@ -765,6 +765,8 @@ If you set the +:readonly+ option to +true+, then the associated object will be
The +:select+ option lets you override the SQL +SELECT+ clause that is used to retrieve data about the associated object. By default, Rails retrieves all columns.
TIP: If you set the +:select+ option on a +belongs_to+ association, you should also set the +foreign_key+ option to guarantee the correct results.
===== +:validate+
If you set the +:validate+ option to +true+, then associated objects will be validated whenever you save this object. By default, this is +false+: associated objects will not be validated when this object is saved.
TIP: You don't need to call +to_xml+ on the object that you want to render. If you use the +:xml+ option, +render+ will automatically call +to_xml+ for you.
==== Rendering Vanilla JavaScript
Rails can render vanilla JavaScript (as an alternative to using +update+ with n +.rjs+ file):
If you're loading multiple javascript files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify +:cache => true+ in your +javascript_include_tag+:
...
...
@@ -601,7 +629,7 @@ You can supply the +:recursive+ option to link files in subfolders of +public/st
If you're loading multiple CSS files, you can create a better user experience by combining multiple files into a single download. To make this happen in production, specify +:cache => true+ in your +stylesheet_link_tag+:
...
...
@@ -766,23 +794,29 @@ This would look for a partial named +_link_area.html.erb+ and render it using th
You can also pass local variables into partials, making them even more powerful and flexible. For example, you can use this technique to reduce duplication between new and edit pages, while still keeping a bit of distinct content:
Although the same partial will be rendered into both views, the label on the submit button is controlled by a local variable passed into the partial.
Every partial also has a local variable with the same name as the partial (minus the underscore). By default, it will look for an instance variable with the same name as the partial in the parent. You can pass an object in to this local variable via the +:object+ option:
Every partial also has a local variable with the same name as the partial (minus the underscore). You can pass an object in to this local variable via the +:object+ option:
@@ -804,6 +838,8 @@ Every partial also has a local variable with the same name as the partial (minus
Within the +customer+ partial, the +@customer+ variable will refer to +@new_customer+ from the parent view.
WARNING: In previous versions of Rails, the default local variable would look for an instance variable with the same name as the partial in the parent. This behavior is deprecated in Rails 2.2 and will be removed in a future version.
If you have an instance of a model to render into a partial, you can use a shorthand syntax:
[source, html]
...
...
@@ -817,15 +853,18 @@ Assuming that the +@customer+ instance variable contains an instance of the +Cus
Partials are very useful in rendering collections. When you pass a collection to a partial via the +:collection+ option, the partial will be inserted once for each member in the collection:
@@ -849,33 +888,42 @@ Rails will render the +_product_ruler+ partial (with no data passed in to it) be
There's also a shorthand syntax available for rendering collections. For example, if +@products+ is a collection of products, you can render the collection this way:
Rails determines the name of the partial to use by looking at the model name in the collection. In fact, you can even create a heterogeneous collection and render it this way, and Rails will choose the proper partial for each member of the collection:
* November 1, 2008: Added +:js+ option for +render+ by link:../authors.html#mgunderloy[Mike Gunderloy]
* October 16, 2008: Ready for publication by link:../authors.html#mgunderloy[Mike Gunderloy]
* October 4, 2008: Additional info on partials (+:object+, +:as+, and +:spacer_template+) by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication)
* September 28, 2008: First draft by link:../authors.html#mgunderloy[Mike Gunderloy] (not yet approved for publication)
As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you'll get +admin_photos_url+ that expects to find an +Admin::PhotosController+ and that matches +admin/photos+, and +admin_photos_ratings+path+ that matches +/admin/photos/_photo_id_/ratings+, expecting to use +Admin::RatingsController+. Even though you're not specifying +path_prefix+ explicitly, the routing code will calculate the appropriate +path_prefix+ from the route nesting.
As you can see, the namespaced version is much more succinct than the one that spells everything out - but it still creates the same routes. For example, you'll get +admin_photos_url+ that expects to find an +Admin::PhotosController+ and that matches +admin/photos+, and +admin_photos_ratings_path+ that matches +/admin/photos/_photo_id_/ratings+, expecting to use +Admin::RatingsController+. Even though you're not specifying +path_prefix+ explicitly, the routing code will calculate the appropriate +path_prefix+ from the route nesting.