Welcome to Giant Robots Smashing Into Other Giant Robots — a weblog about development, business, design and technology — written by thoughtbot.
render right
One extension to Rails ActionController::Base#render method I don’t like is the new :update parameter. This is used to render inline RJS in an action like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class CommentsController < ApplicationController def create @comment = Comment.new params[:comment] respond_to do |wants| if @comment.save wants.js do render :update do |page| page.insert_html :bottom, 'comments', render(:partial => 'comment', :object => @comment) page[:errors].replace_html '' page[:comment_form].reset end end else wants.js do render :update do |page| page[:errors].replace_html @comment.errors.full_messages end end end end end end |
- look at how much longer the action is
- the nesting makes it a pain to read
- in this case the error display will be ugly because I can’t call view methods such as #error_messages_for in a controller so I’m forced to call
ActiveRecord::Errors#full_messages.
I think it’s much better to use RJS views instead. Then the above action becomes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class CommentsController < ApplicationController def create @comment = Comment.new params[:comment] respond_to do |wants| if @comment.save wants.js else wants.js { render :action => 'new.rjs' } end end end end |
1 2 3 4 |
page.insert_html :bottom, 'comments', render(:partial => 'comment', :object => @comment) page[:errors].replace_html '' page[:comment_form].reset |
page[:errors].replace_html error_messages_for(:comment) |
Then you say “but it’s only a couple lines of RJS, why create 2 more files just for that?”. My answer would be: do you ever write normal non-AJAX actions like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class PostsController < ApplicationController def show @post = Post.find params[:id] html =<<-EOS <h2>#{@post.title}</h2> <p>#{@post.body}</p> EOS render :text => html, :layout => 'application' end end |
Of course not.
In my opinion, the :text and :update parameter options in ActionController#Base should both be deprecated.
About this entry
You're reading an entry on GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS, the company weblog of thoughtbot, inc.
- Author:
- Jared Carroll
- Published:
- July 30th 08:29 AM
- Updated:
- September 30th 09:57 AM
- Sections:
- Development
thoughtbot is hiring
We are hiring web developers and web designers in both Boston and New York, NY.
What are we up to?
We built Shoulda, an eclectic set of additions to Test::Unit; Paperclip to manage uploaded files without hassle; Jester, a REST/ActiveResource client library written in Javascript, and Squirrel, an enhancement for ActiveRecord's find syntax; — amongst some other projects.

Chad (President) and Jon (CTO) co-authored a technical book titled Pro Active Record: Databases with Ruby and Rails, which explores the ins and outs of the ActiveRecord ruby library. You can buy it today at Amazon.com.
About thoughtbot, inc.
We are a small web application development consulting business, with offices in Boston, MA and New York, NY. If you're looking to find a team for your next web development project or your new web application — get in touch.
4 comments
Jump to comment form