我创建了_form.html.erb,其中的编码如下
<%= 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 个提交,并且它们都具有诸如“更新”和“销毁”之类的名称。
当用户按下销毁按钮时,books_controller 中的更新操作将被调用。
并判断它是“更新”还是“销毁”请求。
为此,如何在 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]
作为“更新”和当你销毁 你会得到
params[:submit]
作为 Controller 中的“销毁”
关于ruby-on-rails - 如何在 Controller 中获取 f.submit 的名称参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13987512/