redirect or render

Courtenay : March 2nd, 2006

If you want to degrade your ajax requests nicely, you'll need to accept regular ol' POSTS as well as xhr. In these cases, you're probably rendering a partial for an ajax response, and redirecting to a 'show' page for the post (so that they don't get that annoying "do you want to re-post this form" dialog.) Here's a snippet I keep re-using that you may find useful:

  def redirect_or_render( redirect_to_hash, render_page  )  
    if @request.xhr?
      render(render_page)
    else
      redirect_to(redirect_to_hash)
    end
  end
Use it like

redirect_or_render(  
  {:action=>'foo'},
  { :partial => 'monkey', :locals => { :obj = > 'x' } }
)

2 Responses to “redirect or render”

  1. Ben Jackson Says:
    Shouldn't the parameters for that method be in reverse order?
  2. Paul Odeon Says:

    Are you still using this method now that the respond_to method seems to be in vogue? Also I’m curious as to whether you use Ajax redirects, I have had a lot of problems with them.

Leave a Reply

I am a human (check this)

Remember: escape your underscores \_ and indent code at least 4 spaces or incur the wrath of smartypants.