views:

548

answers:

1

I'm using form_remote_for() to do validation on a form's data so that my rails app can show the user errors without reloading the page. I'm having troubles redirecting the page if there are no errors. Here is my code:

def create
  # Validate the data
  if error_count > 0
    render :update do |page|
      page.replace_html(div_id, error_text)
    end
  else
    # Save the data
    redirect_to(page_to_go_to)
  end
end

Everything in this method works fine except the redirect. The server claims it does a successful redirect and I believe it's the AJAX call that's being redirected. What I want to happen is have the page that contains the form to redirect. Is that possible?

+3  A: 

I thought about this some more and realized that I needed the page to redirect instead of the request I was handling. So I tried the following and it works great.

def create
  # Validate the data
  if error_count > 0
    render :update do |page|
      page.replace_html(div_id, error_text)
    end
  else
    # Save the data
    render :update do |page|
      page.redirect_to(page_to_go_to)
    end
  end
end
Algorithmic