Rafael Noronha

software development and technology

Render update with helper methods

without comments

At this post I’ll show you how I have been responding to Ajax requests at web apps built on top of rails 2.3.

Ajax completely changed the web experience, and I highly recommend you to embrace it whenever your requirements are not disallowing you.

For the sake of curiosity, at my current project probably more than 90% of post/put/delete requests are performed asynchronously.

If you don’t know the way Rails can handle that, it is a very simple concept.

You write some Ruby code that will generate javascript code, being this js code executed when the request is responded with success.

We can do that writing rjs files, or, my preferred method, we can generate the code directly from a controller call.

This could bring us some SRP problems, if we were writing this code at the controller class.

But my approach is encapsulating the response code at helper methods, because that should be the home of any code taking care of html concerns.

I think this is a good way to keep my code well organized so anyone can easily identify how things are supposed to work looking to the helper methods, without being required to open a lot of rjs files.

At this gist you can see a sample code of this approach.

Do you prefer the rjs option?
Maybe I’m going against one of those Rails conventions?


Help me to spread the word!
  • Digg
  • del.icio.us
  • RSS
  • DotNetKicks
  • Technorati
  • Twitter
  • FriendFeed

Written by rafanoronha

September 8th, 2009 at 12:29 pm

Posted in Sem categoria

Tagged with

Leave a Reply