views:

45

answers:

2

In my rails app, I need to submit a form via ajax so that the form submit happens behind the scenes and doesn't require a full page reload.

I did form_remote_tag, but I can't figure out how to get a response from it make sure it was successful. Is there a way to, or do people just trust it's always successful (eek!)?

Or is there a better solution?

A: 

When you're responding to the AJAX request - probably from a the create or update action of one of your controllers, you should respond with some javascript or JSON.

If you create an .rjs file to respond, it will get executed when the page responds. If everything went well, you can redirect or update the page. If something failed, replace the form with a partial which will display the errors. Read about rjs here: http://www.codyfauser.com/2005/11/20/rails-rjs-templates

Jamie Wong
Thanks, I'm trying to respond with json now but it causes the browser to throw up a 'save' dialog. I guess it's not expecting json -- if I only knew why.
99miles
A: 

Just bind a callback to your form_remote_tag that would process your response.

It works fine for me. Hope will help you out.

Here my code :

<% form_remote_tag :url => some_path, :method=>'post',:complete =>'your_callback_method();' do %>
Dinesh Atoliya