Rails 1.2 RC1: New in Action Pack

Posted by josh November 26, 2006 @ 04:32 PM

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

Posted in Releases | 10 comments

Comments

  1. Peter on 27 Nov 17:09:

    Isn’t the filter parameter logging already in 1.1.6? filter_parameter_logging “password”

    Is it somehow different from what I am using right now?

  2. topfunky on 27 Nov 19:59:

    I used the changelog from the last official release, but filter_parameter_logging may have slipped in with the security update.

    At any rate, yes, it’s the same thing.

  3. Peter on 27 Nov 20:46:

    Yes, it was part of the security update. :)

  4. asdfasfd@gmail.com on 28 Nov 03:42:

    why did you all decide to remove the assumed png suffix, i really liked that, and have bunches of views with that symantic ?

  5. topfunky on 28 Nov 06:39:

    I don’t speak for the core team, but you can get that functionality back with about 4 lines of Ruby. I’m sure someone will release a plugin that does that.

  6. DHH on 28 Nov 07:04:

    asdfasfd: Primarily because you can desire dynamic images that are generated from actions without extensions.

  7. topfunky on 28 Nov 17:36:

    Actually, I’m looking forward to the release of 1.2 because I will be able to generate images of different types based on the :format. So the extension is actually more useful for generating dynamic images, in my opinion.

  8. Peter on 28 Nov 18:37:

    I liked being able to name the images following a convention like model-action, and then having my code dynamically use the appropriate image for whichever model and action was at hand. Now I’ll have to add the .png. No biggie I guess.

  9. Kevin on 06 Dec 20:43:

    “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).””

    and

    “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.”

    Are fantastic improvements! I’ve been waiting for them both. Thanks so much!

  10. Marshall on 08 Dec 06:33:

    “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 how do we make it show a 404 for invalid URLs? Right now all bad addresses are showing 500s for me. :(