Rails 1.2 RC1: New in Action Pack

With all respect to the reporter from the Edge, here are a few tasty bits from ActionPack in Rails 1.2 (CHANGELOG). (compiled by Geoffrey Grosenbach).

Views

You can now access nested attributes in RJS:

page['foo']['style']['color'] = 'red' # => $('foo').style.color = 'red';

Forms now use blocks instead of end_form_tag (notes from DHH):


<% form_tag(products_url) do %>
  <%= text_field :product, :title %>
  <%= submit_tag "Save" %>
<% end -%>

And how many blogs have you visited that say “Last updated 60 days ago”? Years and months have been added to distance_of_time_in_words, so you’ll see “2 months ago” or maybe even “5 years ago” now.

Controllers

Uncaught exceptions raised anywhere in your application will cause RAILS_ROOT/public/500.html 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!

There is a new head(options = {}) method for responses that have no body.

head :status => 404 # return an empty response with a 404 status
head :location => person_path(@person), :status => 201

You can declare specific file extensions exempt from layouts. Bring on the CSS, PDF, and graphic generating plugins!

ActionController::Base.exempt_from_layout 'rpdf'

RESTful resources automatically get a params[:format] 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 (cheat sheet).

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

You can also register your own custom MIME types. These will be automatically incorporated into controllers so you can use them in respond_to blocks and as file :format extensions.

Mime::Type.register(string, symbol, synonyms = [])
Mime::Type.register("image/gif", :gif)

Finally, ActionController.filter_parameter_logging makes it easy to remove passwords, credit card numbers, and other sensitive information from being logged when a request is handled.

filter_parameter_logging 'password' # Don't log fields that match 'password'

Routing and URLs

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.


class MyMailer < ActionMailer::Base

  include ActionController::UrlWriter
  default_url_options[:host] = 'my_site.com'

Testing

assert_response now supports additional symbolic status codes.

  assert_response :success # You know this one
  assert_response :ok
  assert_response :not_found
  assert_response :forbidden

Added the rulin’ assert_select for CSS selector-based testing (cheat sheet). Use this instead of assert_tag from now on.

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"

Deprecated

You’ll see warnings when you run your test suite. Here are a few that have been replaced with better syntax:

  • assert_tag → assert_select
  • start_form_tag and end_form_tag → form_tag do end
  • @cookies, @headers, @request, @response, @params, @session, @flash → cookies, headers, request, response, params, session, flash
  • .png is no longer automatically appended to extension-less image_tag calls
blog comments powered by Disqus