29 Nov, 2006

Published at 05:32PM

Tagged with development, api, webapps, rails, and ajax

This post has 2 comments

The respond_to option in Rails

See, this is the type of thing I sometimes don’t realize. I haven’t used respond_to in any of my applications, and I’ve implemented quite a bit of Ajax (not that respond_to is solely used for Ajax). And for the record, I’ve never attempted to write my very own REST web-service in Rails. I’ve requested other web-service’s, but I’ve never supplied an API. But it’s seemingly ridiculous to not include it in some of the things I do. You know, just in case. And if nothing amounts of it, at least I’ll know what needs done if I ever do need to do it.

There are two ways I’ve dealt with Ajax in Rails: the 1.0 way of render_without_layout and the 1.1 way of RJS. In either case I had an extra method (which means an extra template/view) to handle the Ajax response (I also wasn’t aware of xhr?). So, what resond_to is trying to tell me is that I can use the same method/action for regular post-back’s, Ajax request’s, and REST requests??? And not to mention it’s only 5 (or so) lines of code? That’s quite impressive, unless of course, I’m not stating things correctly. From all that I’ve read, I’ve somehow missed the boat on the respond_to option. I think I noticed it a time or two, but never dug deeper—it looks very handy. Are those three classifications (html, js, xml) the main usages with respond_to, or are there more?

Comments

Chris Thursday, 30 Nov, 2006 Posted at 05:14AM

You can use respond_to for all sorts of stuff, including RSS output. The syntax gets a bit crazy if you ever want to do inline stuff:

respond_to do |wants|
  wants.js do 
    render :update do
      # rjs code
    end
  end
end

I believe that’s how it works.

Ryan Thursday, 30 Nov, 2006 Posted at 08:09AM

Have you used the RESTful approach much? I haven’t done anything with it yet, but I imagine you started using it right when it hit Edge Rails. It’s packaged with 1.2 right? I don’t know that I’m completely following its true benefits. Is it just less code and easier to maintain? Or is it much more than that? If you had to summarize the benefits of a RESTful controller, what would you say?

Do you have something to say about this post?
Retype the image to the right Spam Hint: Are You Human? Textile Formatting Tips

or

Ryan Heath | Site Management A Ruby on Rails production.

This site is a Formed Function. Formed Function LLC | @formedfunction | Get in Touch