十分尊重 Edge 报告人员,以下列出 Rails 1.2 中 ActionPack 的几个巧妙之处(CHANGELOG)。(由 Geoffrey Grosenbach 汇编)
<h1>Views</h1>
<p>You can now access nested attributes in <span class="caps">RJS</span>:</p>
<pre><code>page['foo']['style']['color'] = 'red' # => $('foo').style.color = 'red';</code></pre>
<p>Forms now use blocks instead of <code>end_form_tag</code> (<a href="http://www.loudthinking.com/arc/000601.html">notes from <span class="caps">DHH</span></a>):</p>
<% form_tag(products_url) do %>
<%= text_field :product, :title %>
<%= submit_tag "Save" %>
<% end -%>
<p>And how many blogs have you visited that say “Last updated 60 days ago”? Years and months have been added to <code>distance_of_time_in_words</code>, so you’ll see “2 months ago” or maybe even “5 years ago” now.</p>
<h1>Controllers</h1>
<p>Uncaught exceptions raised anywhere in your application will cause <code>RAILS_ROOT/public/500.html</code> to be read and shown instead of just the static “Application error (Rails).” So make it look nice if you aren’t using it already!</p>
<p>There is a new <code>head(options = {})</code> method for responses that have no body.</p>
<pre><code>head :status => 404 # return an empty response with a 404 status head :location => person_path(@person), :status => 201</code></pre>
<p>You can declare specific file extensions exempt from layouts. Bring on the <span class="caps">CSS</span>, PDF, and graphic generating plugins!</p>
<pre><code>ActionController::Base.exempt_from_layout 'rpdf'</code></pre>
<p>RESTful resources automatically get a <code>params[:format]</code> option that can force a content type. If :format is specified and matches a declared extension, that mime type will be used in preference to the “Accept” header. This means you can link to the same action from different extensions and use that fact to determine output (<a href="http://nubyonrails.com/articles/2006/10/09/peepcode-rest-basics">cheat sheet</a>).</p>
<pre><code>class WeblogController < ActionController::Base def index
@posts = Post.find :all
respond_to do |format|
format.html
format.xml { render :xml => @posts.to_xml }
format.rss { render :action => "feed.rxml" }
end end</code></pre>
<p>You can also register your own custom <span class="caps">MIME</span> types. These will be automatically incorporated into controllers so you can use them in <code>respond_to</code> blocks and as file <code>:format</code> extensions.</p>
<pre><code>Mime::Type.register(string, symbol, synonyms = []) Mime::Type.register("image/gif", :gif)</code></pre>
<p>Finally, <code>ActionController.filter_parameter_logging</code> makes it easy to remove passwords, credit card numbers, and other sensitive information from being logged when a request is handled.</p>
<pre><code>filter_parameter_logging 'password' # Don't log fields that match 'password'</code></pre>
<h1>Routing and URLs</h1>
<p>Routing has been significantly rewritten for speed and consistency. One of the benefits is that you can use named routes and RESTful routes in your mailer templates.</p>
class MyMailer < ActionMailer::Base
include ActionController::UrlWriter
default_url_options[:host] = 'my_site.com'
<h1>Testing</h1>
assert_response
现支持额外的符号状态代码。
assert_response :success # You know this one
assert_response :ok
assert_response :not_found
assert_response :forbidden
<p>Added the rulin’ <code>assert_select</code> for <span class="caps">CSS</span> selector-based testing (<a href="http://blog.labnotes.org/2006/09/04/assert_select-cheat-sheet/">cheat sheet</a>). Use this instead of <code>assert_tag</code> from now on. </p>
<pre><code>assert_select "a[href=http://assert_select_rules.com]", @item.url, "Should have a link" assert_select "div#products", nil, "Should show a products div on the page"</code></pre>
<h1>Deprecated</h1>
<p>You’ll see warnings when you run your test suite. Here are a few that have been replaced with better syntax:</p>
<ul>
<li>assert_tag → assert_select</li>
</ul>
<ul>
<li>start_form_tag and end_form_tag → form_tag do end</li>
</ul>
<ul>
<li>@cookies, @headers, @request, @response, @params, @session, @flash → cookies, headers, request, response, params, session, flash</li>
</ul>
<ul>
<li>.png is no longer automatically appended to extension-less <code>image_tag</code> calls</li>
</ul>