内容简介:翻译自:https://stackoverflow.com/questions/7048843/form-for-with-multiple-controller-actions-for-submit
如何在form_for提交时传递网址?我正在尝试使用一个表单,每个按钮指向每个控制器操作,一个是搜索,另一个是创建.是否可以在同一表单上设置2个具有不同操作的提交按钮?
<%= form_for @people do |f| %> <%= f.label :first_name %>: <%= f.text_field :first_name %><br /> <%= f.label :last_name %>: <%= f.text_field :last_name %><br /> <%= f.submit(:url => '/people/search') %> <%= f.submit(:url => '/people/create') %> <% end %>
根据按下的按钮,没有简单的Rails方法将表单提交到不同的URL.您可以在每个按钮上使用javascript来提交到不同的URL,但处理这种情况的更常见方法是允许表单提交到一个URL并检测控制器操作中按下了哪个按钮.
对于第二种方法,使用这样的提交按钮:
<%= form_for @people do |f| %> # form fields here <%= submit_tag "First Button", :name => 'first_button' %> <%= submit_tag "Second Button", :name => 'second_button' %> <% end %>
你的行动看起来像这样:
def create if params[:first_button] # Do stuff for first button submit elsif params[:second_button] # Do stuff for second button submit end end
有关这两种方法的更多信息,请参见 this similar question
另请参阅多按钮表单上的 Railscast episode 38 .
翻译自:https://stackoverflow.com/questions/7048843/form-for-with-multiple-controller-actions-for-submit
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- iOS之导航返回上上个控制器或指定返回某个控制器
- iOS小技巧·把子视图控制器的视图添加到父视图控制器
- javascript – AngularJS控制器错误 – :$http.get不是控制器部分的函数
- AC敏捷控制器及准入控制技术对比
- angularjs – 使用ui-router时,控制器是否可以从父控制器继承范围
- SpringMVC: 前端控制器
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Security Testing Cookbook
Paco Hope、Ben Walther / O'Reilly Media / 2008-10-24 / USD 39.99
Among the tests you perform on web applications, security testing is perhaps the most important, yet it's often the most neglected. The recipes in the Web Security Testing Cookbook demonstrate how dev......一起来看看 《Web Security Testing Cookbook》 这本书的介绍吧!