views:

31

answers:

1

I want to render a form (fields and includes a submit button) when using link_to_remote -- the form appears via an ajax call.

I'd like that form to behave as any other form would (it's a basic New Active Record) form, nothing fancy.

Upon submit, though, I'd like the original parent page to remain static without refreshing and, instead of the form, the notice (your form has been successfully submitted) in its place.

Current behavior, upon submit, it redirects the entire page.

I put the remote into a method:

<%= do_event_remote(contact_event, event) %>

And here is this helper method:

  def do_event_remote(contact, call_or_email_or_letter) 
    model_name = call_or_email_or_letter.class.name.tableize.singularize 
    link_to_remote "#{model_name.camelize}", 
                   :url => send("new_contact_#{model_name}_path", 
                                            :contact => contact, 
                                            :status => 'done',
                                            :"#{model_name}" => call_or_email_or_letter ),
                   :update => 'remote_event'                                     
  end 

Thanks...

A: 

You create the form as normal but you use the remote_form_for helper in the place of the form_for and the remote_form_tag in the place of form_tag. This will submit the form via ajax.

Documenation at: http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html

Update: Example:

In your form (using haml):

= remote_form_for @customer, :url => customer_path(@customer), :complete => "doSomething(request)" do |f|
  %p
    First Name<br>
    = f.text_field :first_name
  %p
    Last Name<br>
    = f.text_field :last_name
  = submit_tag "Update"

For your fade functions, you would need to use scriptaculous or jquery. Scriptaculous comes packaged with rails and is currently the default. For that, your javascript would be something like this:

function doSomething(request){
  Effect.fade('some_div');
}

more on scriptaculous: http://wiki.github.com/madrobby/scriptaculous/

Geoff Lanotte
okay...so I still use the link_to_remote, but in the _form html which uses the form_for, replace per your notes?
Angela
no, instead of the form_for, use form_for_remote. But you have options similar to link_to_remote for ajax options. (update, etc)
Geoff Lanotte
Ah, and you just use a regular old submit tag to submit the form
Geoff Lanotte
so in the _form, I use form_for_remote, right? Sorry, just want to make sure.
Angela
What happens to the form after it gets submitted? How can I define it and show degradation (fade out) using prototype?
Angela
I added a little javascript, to show how you would call it. And *blush* it is `remote_form_for` and _not_ `form form_for_remote`
Geoff Lanotte
right, hi, I knew what you were meaning, so no worries...however, I put that into my _form.erb.html file remote_form_for and replaced the existing form...oh, wait...need to use submit_tag...will try that back...thanks so much for the scriptaculous on how to do the fade out on the form....
Angela