psic
psic

Reputation: 359

Calling Rails Controller/action submit form from javascript?

Here is my rails form in my view

<%= form_tag :action=> "valider" do %>
....
<%= submit_tag  "Valider", {:class => 'bouton', :onclick=>"valider_form()"} %>
...<% end %>

it called the valider_form javascript function and then the valider action of the controller to store data in database.

When user leaves the page with an non-empty form, we propose to store his data and we want to call the valider_form() javascript function and the valider action of the controller. We use the OnUnload javascript event, and it's easy to call the valider_form() javascript function. But, We don't know how to call in javascript the valider action, or simulate this post event.

Upvotes: 0

Views: 1254

Answers (2)

jake
jake

Reputation: 1691

Try this:

<%= form_tag url_for(:controller => 'controller_name', :action => :valider),
:remote => true do %>
    ...
<% end %>

The :remote => true option makes Rails submit the request dynamically with AJAX, calling the :valider method in the 'controller_name' controller.

Upvotes: 0

psic
psic

Reputation: 359

Add an id to my form :

<%= form_tag  '/mon_equipe/valider', :id=>'equipe' do %>

In my javascript OnUnload event :

valider_form();
var form = document.getElementById("equipe");
form.submit();

Upvotes: 0

Related Questions