{"id":2451,"date":"2012-01-26T14:31:32","date_gmt":"2012-01-26T16:31:32","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=2451"},"modified":"2012-03-08T10:05:55","modified_gmt":"2012-03-08T13:05:55","slug":"my-five-favorite-hidden-features-in-rails-3-2","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2012\/01\/my-five-favorite-hidden-features-in-rails-3-2\/","title":{"rendered":"My five favorite “hidden” features in Rails 3.2"},"content":{"rendered":"
Rails 3.2 is out with great features on spotlight<\/a>: faster development reloading, faster router and explain queries. However, every Rails release ships with minor features that do not get that much attention but still would be a great fit to your application. This blog post is about my five favorites “hidden” features of Rails 3.2.<\/p>\n This feature written by Prem Sichanugrist<\/a> provides a very simple but welcome clean up to your views. Both You can simply write:<\/p>\n It is funny how some parts of Rails as old as the migration generators continue receiving improvements day after day. Rails 3.1 already added a feature that automatically generate indexes for associations, by simply invoking:<\/p>\n <\/p>\n <\/p>\n With the above, Rails will detect that post is a reference and it will automatically 1) add a Right after 3.1 came out, I have pushed another small feature to the migration generator that simply makes the type attribute default to string. Therefore, you no longer need to write:<\/p>\n <\/p>\n <\/p>\n You could simply write:<\/p>\n <\/p>\n <\/p>\n Oddly enough, the idea for this feature came when I was preparing a presentation and the scaffold command could not fit in a slide (the so-called Presentation Driven Development). Anyhow, this small addition would not be enough to make to the best five “hidden” features of Rails 3.2. That’s when Dmitrii Samoilov comes in.<\/p>\n Dmitrii sent a pull request<\/a> that allows you to specify which columns should have an (unique) index. So one could write:<\/p>\n <\/p>\n <\/p>\n And the generator will automatically generate an index for name and an unique index for e-mail. There are other features there as well, so don’t forget to checkout the CHANGELOG.<\/p>\n When Rails 3.0 came out, one of the features that people suddenly missed was the ability to better handle exceptions. The issue was: since Rails 3 became a lot more Rack “fluent”, we had to move some features to the middleware stack and this forced us to move the whole exceptions handling as well. Rails 3.2 attempts to bring some customization back to the game by allowing you to set your own exceptions rack application that is invoked when a failure happens. For instance, you could set the exceptions application to your own router in your Now, every time there is an exception, your router is going to be invoked. Therefore, to render custom 404 pages, you could simply add to your router:<\/p>\n And implement the logic in the controller as you wish! However, there are a few things to keep in mind if you go down this road:<\/p>\n Remember that whatever you do in the errors controller, it should not be anything “fancy”. Keep it simple because something already went wrong with your application!<\/p>\n1) Smarter
content_tag_for<\/code><\/h3>\n
content_tag_for<\/code> and
div_for<\/code> now accepts an array of records and automatically loop over each record. Therefore, instead of writing this:<\/p>\n
\r\n @posts.each do |post|\r\n content_tag_for(:li, post) do\r\n ...\r\n end\r\n end\r\n<\/pre>\n
\r\n content_tag_for(:li, @posts) do |post|\r\n ...\r\n end\r\n<\/pre>\n
2) Smarter migration generators<\/h3>\n
\r\nrails g scaffold Comment post:references title:string body:text\r\n<\/pre>\n
post_id<\/code> integer column, 2) add an association to your model and 3) add an index to that column.<\/p>\n
\r\nrails g scaffold Person name:string email:string\r\n<\/pre>\n
\r\nrails g scaffold Person name email\r\n<\/pre>\n
\r\nrails g scaffold Person name:index email:uniq\r\n<\/pre>\n
3) Flexible exception handling<\/h3>\n
config\/application.rb<\/code>:<\/p>\n
config.exceptions_app = self.routes<\/pre>\n
match \"\/404\", :to => \"errors#not_found\"<\/pre>\n
\n
match<\/code> in your routes and not
get\/post\/put\/delete<\/code> because such exceptions can happen in any HTTP request;<\/li>\n
config.consider_all_requests_local<\/code> to false in your
config\/environments\/development.rb<\/code>. The reason is, if the request is considered local, Rails will always favor to show the debug exceptions page;<\/li>\n
env[\"action_dispatch.exception\"]<\/code>;<\/li>\n
public\/STATUS.html<\/code> is available here: action_dispatch\/middleware\/public_exceptions.rb<\/a><\/li>\n<\/ol>\n
4) Custom partial paths<\/h3>\n