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
That’s a very simple AJAX comment submission. Instead of creating RJS templates in app/views/comments I just rendered the RJS inline in the action. I think this is bad because
  1. look at how much longer the action is
  2. the nesting makes it a pain to read
  3. 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
create.rjs
1
2
3
4
  page.insert_html :bottom, 'comments', render(:partial => 'comment',
                                               :object => @comment)
  page[:errors].replace_html ''
  page[:comment_form].reset
new.rjs

  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

 

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.