简体   繁体   English

从JavaScript调用Rails Controller /动作提交表单?

[英]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. 它调用了validateer_form javascript函数,然后调用了控制器的valider操作,以将数据存储在数据库中。

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. 当用户使用非空格式离开页面时,我们建议存储他的数据,并且我们要调用valider_form()javascript函数和控制器的validateer操作。 We use the OnUnload javascript event, and it's easy to call the valider_form() javascript function. 我们使用OnUnload javascript事件,可以很容易地调用valider_form()javascript函数。 But, We don't know how to call in javascript the valider action, or simulate this post event. 但是,我们不知道如何在javascript中调用验证程序操作或模拟此post事件。

Add an id to my form : 在我的表单中添加一个ID:

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

In my javascript OnUnload event : 在我的javascript OnUnload事件中:

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

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. :remote => true选项使Rails使用AJAX动态提交请求,并在'controller_name'控制器中调用:valider方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM