ruby-on-rails – form_for,具有多个控制器操作以进行提交

栏目: Ruby · 发布时间: 6年前

内容简介:翻译自: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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Security Testing Cookbook

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》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具