问题描述
我创建了_form.html.erb,其中的编码如下
I created _form.html.erb, in which it's coded like this
<%= form_for @book, :html => { :class => 'form-horizontal' } do |f| %>
....
<%= f.submit nil, :class => 'btn btn-primary', :name => 'update' %>
<%= f.submit 'Destroy', :class => 'btn btn-danger', :name => 'destroy' %>
....
<% end %>
是的,我确实以相同的形式提交了 2 个提交,并且它们都具有诸如更新"和销毁"之类的名称.
Yes, I do have 2 submits in the same form, and both of them have name such as 'update' and 'destroy'.
当用户按下销毁按钮时,books_controller 中的更新动作将被调用.
并判断它是更新"还是销毁"请求.
为此,我如何在控制器中获取其名称参数?
When the user press destroy button, update action in books_controller will be called.
and it judges whether if it's 'update' or 'destroy' request.
For that, how can I get its name parameter in controller?
更新操作将是这样的
def update
if params[:books][:name] == 'destroy'
destroy transaction
else
update transaction
end
end
推荐答案
试试这个,
<%= f.submit 'Update', :class => 'btn btn-primary', :name => 'submit' %>
<%= f.submit 'Destroy', :class => 'btn btn-danger', :name => 'submit' %>
现在,当您更新时,您将获得 params[:submit]
作为更新"和当您 Destroy 时,您将在控制器中将 params[:submit]
作为Destroy"
Now when you Update you will get params[:submit]
as "Update" and when you Destroy you will get params[:submit]
as "Destroy" in your controller
这篇关于如何在控制器中获取 f.submit 的名称参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!