form_for 具有多个用于提交的控制器操作

发布于 11-29 15:36 字数 469 浏览 3 评论 0原文

如何在 form_for 提交中传递 url?我尝试使用一种表单,每个按钮都指向每个控制器操作,一个是搜索,另一个是创建。是否可以在同一个表单上有 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 %>

How do I pass a url on the form_for submit? I'm trying to use one form with each buttons pointing to each controller actions, one is search and another one is create. Is it possible to have 2 submit buttons with different actions on the same form?

<%= 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 %>

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

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

发布评论

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

评论(3

顾北清歌寒2024-12-06 15:36:51

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

请参阅 这个类似的问题了解有关这两种方法的更多信息。

另请参阅有关多按钮表单的Railscast Episode 38

There is not a simple Rails way to submit a form to different URLs depending on the button pressed. You could use javascript on each button to submit to different URLs, but the more common way to handle this situation is to allow the form to submit to one URL and detect which button was pressed in the controller action.

For the second approach with a submit buttons like this:

<%= form_for @people do |f| %>
  # form fields here
  <%= submit_tag "First Button", :name => 'first_button' %>
  <%= submit_tag "Second Button", :name => 'second_button' %>
<% end %>

Your action would look something like this:

def create
  if params[:first_button]
    # Do stuff for first button submit
  elsif params[:second_button]
    # Do stuff for second button submit
  end
end

See this similar question for more about both approaches.

Also, see Railscast episode 38 on multibutton forms.

伪装你2024-12-06 15:36:51

这个问题与 非常相似这个,虽然有点不同,但

我只是想强调一下,上述问题的一些答案也建议向路线添加约束,这样您实际上可以将查询路由到不同的控制器操作!

感谢作者 vss123

我们使用 Rails 中的高级约束来解决问题。

这个想法是拥有相同的路径(因此具有相同的命名路线和路径)。
动作),但具有路由到不同动作的约束。

resources :plan do   
  post :save, constraints: CommitParamRouting.new("Propose"), action: :propose
  post :save, constraints: CommitParamRouting.new("Finalize"), action: :finalize 
end

CommitParamRouting 是一个简单的类,有一个方法匹配?哪个
如果提交参数与给定的实例属性匹配,则返回 true。
值。

这可以作为 gem commit_param_matching 使用。

This question is very similar to this one, though it's a bit different

I just wanted to higlight that some answer to the aforementionned question also suggested to add constraints to the routes, so you can actually route the queries to different controller actions !

Credits to the author, vss123

We solved using advanced constraints in rails.

The idea is to have the same path (and hence the same named route &
action) but with constraints routing to different actions.

resources :plan do   
  post :save, constraints: CommitParamRouting.new("Propose"), action: :propose
  post :save, constraints: CommitParamRouting.new("Finalize"), action: :finalize 
end

CommitParamRouting is a simple class that has a method matches? which
returns true if the commit param matches the given instance attr.
value.

This available as a gem commit_param_matching.

风追烟花雨2024-12-06 15:36:51

我面临着完全相同的问题。

我有一个创建表单。但是,如果记录已经存在,我想通过搜索结果显示结果。

选项 1:有两个单独的表单

有两个单独的表单:

<%= form_for @people do |f| %>
    <%= f.submit(:url => '/people/search') %>    
<% end %>

当您提交上面的表单时,编写一些 javascript 来同时提交下面的表单。

<%= form_for @people do |f| %>
    <%= f.submit(:url => '/people/search') %>    
<% end %>

选项 2:使用一张带有两个按钮的表格

  • 正如 Cyril Duchon-Doris 所说。
  • 隐藏搜索按钮。使用刺激控制器操作通过搜索按钮触发提交。
  • Turbo 流结果。

I'm facing the exact same problem.

I have a create form. However, if the record already exists, I want to show the results via a search results.

Option 1: Have two separate forms

Have two separate forms:

<%= form_for @people do |f| %>
    <%= f.submit(:url => '/people/search') %>    
<% end %>

When you submit the above form, write some javascript to also submit the form below.

<%= form_for @people do |f| %>
    <%= f.submit(:url => '/people/search') %>    
<% end %>

Option 2: Have one form with two button

  • Like Cyril Duchon-Doris says.
  • Hide the search button. Use a stimulus controller action to trigger submissions via the search button.
  • Turbo stream the results.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文