Rails:从submit_tag更改表单操作

发布于 2024-10-04 06:56:29 字数 818 浏览 0 评论 0原文

我有一个表单,它始终将表单提交到控制器中的“更新”功能,并且我使用“remote_form_for”标签创建了此表单。在此表单中,我有来自不同表的对象,并且我想通过 AJAX 请求将整个表单数据提交到另一个函数(而不是“更新”函数)。

我尝试了很多方法,包括使用带有操作的提交标签

<% remote_form_for @employee, :url => organization_employee_path(@organization, @employee), :method => :put do |employee_form| %>
  // form objects and other functionalities
     ....
     ....  
         // views inside the forms
         <div id="employee_header_div">
           <%= render :partial => "employee_header", :locals => {:employee => @employee} %>
         </div>
         ...
         ... 
     <%= submit_tag "page_level_validation", :id => "page_level_validation" , :action=>"validate"%>
 <% end %>

,但 Ajax 请求总是调用相同的“更新”函数。

如果有人帮助解决这个问题,那将非常有帮助。

I have a form which is always submitting the form to the "update" function in the controller, and I have created this form using "remote_form_for" tag. In this form I have objects from different tables, and from this form I want to submit entire form data to another function(not to the "update" function) via AJAX request.

I have tried many methods including using submit tag with action

<% remote_form_for @employee, :url => organization_employee_path(@organization, @employee), :method => :put do |employee_form| %>
  // form objects and other functionalities
     ....
     ....  
         // views inside the forms
         <div id="employee_header_div">
           <%= render :partial => "employee_header", :locals => {:employee => @employee} %>
         </div>
         ...
         ... 
     <%= submit_tag "page_level_validation", :id => "page_level_validation" , :action=>"validate"%>
 <% end %>

But the Ajax request always calling the same "update" function.

It would be very helpful, if anyone helps to resolve this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瑾夏年华 2024-10-11 06:56:29

您不能将提交设置为指向主表单指定的不同位置(除非您想使用 HTML5 formaction 属性并处理浏览器兼容性后果)。

但是,您可以做的是在控制器中创建一个新操作来处理这种情况。

例如。

<% remote_form_for @employee, :url => organization_employee_validate_path(@organization, @employee), :method => :put do |employee_form| %>

在你的控制器中

def validate
    #do something loosely based around the update method
end

不要忘记添加适当的路由。

You can't set the submit to point to a different place than the main form has specified (unless you want to use the HTML5 formaction attribute and deal with the browser compatibility consequences).

However, what you could do is create a new action in your controller which deals with the situation.

e.g..

<% remote_form_for @employee, :url => organization_employee_validate_path(@organization, @employee), :method => :put do |employee_form| %>

in your controller

def validate
    #do something loosely based around the update method
end

not forgetting to add the appropriate routes.

烟凡古楼 2024-10-11 06:56:29

试试这个:

<% form_for @employee, :remote => true, :url => organization_employee_path(@organization, @employee), :method => :put do |employee_form| %>

Try this:

<% form_for @employee, :remote => true, :url => organization_employee_path(@organization, @employee), :method => :put do |employee_form| %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文