将同一表单提交给不同的操作 [英] Submit same form to different actions

查看:182
本文介绍了将同一表单提交给不同的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

取决于用户选择的 Cat Dog code>在这种情况下),我想使用正确的http动词将表单提交给正确的控制器操作。



在这种情况下,一个文本输入。如果用户按 Cat 我想要 POST Cats#create 如果 PUT 狗#update

我将如何构建表单?

.stack.imgur.com / jL1C5.pngrel =nofollow noreferrer>

编辑



使用 formaction 属性我可以将 PUT 改为狗#update

 <%= form_for(cat)do | f | %GT; 

< div class =field>
<%= f.label:name%>
<%= f.text_field:name%>
< / div>


< div class =actions>
<%= f.submitCat%>
<%= f.submitDog,formaction:dog_path(Dog.first)%>
<%#= f.submit猫,名称:'route_to [cat]'%>
<%#= f.submitDog,名称:'route_to [dog]'%>
< / div>
<%end%>

问题是我想要 POST Dogs#create ,这可能与 formaction



编辑



有一个 railscast 关于基于按下按钮的名称构建控制器的逻辑。但我想把关于哪个HTTP动词,控制器和动作放在提交按钮本身的逻辑上。这可能吗?

解决方案

在这种情况下,创建 Cat Dog 足够相关,所以在 CatsController 中处理案例陈述

a>,并在 submit 按钮上使用:name 属性。



例如:

app / views / cats / new.html.erb:

 <%= form_tag(cats_path,multipart:true,role:form)do%> 
<%= button_tag'猫',名称:'route_to [cat]'%>
<%= button_tag'狗',名称:'route_to [狗]'%>
<%end%>

app / controllers / cats_controller.rb:

  def创建
case route_to params
当:cat
#创建一个cat
时:dog
#更新狗
结束
结束



private
$ b def route_to params
params [:route_to] .keys.first.to_sym
end


Depending on which submit button a user selects, (Cat and Dog in this case), I'd like to submit the form to the right controller action using the right http verb.

In this case, there is one text input. If the user presses Cat I'd like to POST to Cats#create and if Dog to PUT to Dogs#update.

How would I build the form?

Edit

Using the formaction attribute I'm able to PUT to Dogs#update

<%= form_for(cat) do |f| %>

  <div class="field">
    <%= f.label :name %>
    <%= f.text_field :name %>
  </div>


  <div class="actions">
    <%= f.submit "Cat" %>
    <%= f.submit "Dog", formaction: dog_path(Dog.first) %>
    <%#= f.submit "Cat", name: 'route_to[cat]' %>
    <%#= f.submit "Dog", name: 'route_to[dog]' %>
  </div>
<% end %>

The problem is I want to POST to Dogs#create, is this possible with formaction?

Edit

There is a railscast about building the logic the controller based on the name of the button pressed. But I want to put the logic about which HTTP verb, controller, and action in the submit button itself. Is this possible?

解决方案

In this case creating a Cat and updating a Dog were related enough that it makes sense to handle both in the CatsController with a case statement and use a :name attribute on on the submit button.

For example:

app/views/cats/new.html.erb:

<%= form_tag(cats_path, multipart: true, role: "form") do %>
  <%= button_tag 'Cat', name: 'route_to[cat]'%>
  <%= button_tag 'Dog', name: 'route_to[dog]' %>
<% end %>

app/controllers/cats_controller.rb:

def create
  case route_to params
  when :cat
    # create a cat
  when :dog
    # update a dog
  end
end
.
.
.
private

  def route_to params
    params[:route_to].keys.first.to_sym
  end

这篇关于将同一表单提交给不同的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆