How can I get f.submit's name parameter in controller?
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 %>
Yes, I do have 2 submits in the same form, and both of them have name such as 'update' and 'destroy'.
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?
update action will be something like this
def update
if params[:books][:name] == 'destroy'
destroy transaction
else
update transaction
end
end
Try this,
<%= f.submit 'Update', :class => 'btn btn-primary', :name => 'submit' %>
<%= f.submit 'Destroy', :class => 'btn btn-danger', :name => 'submit' %>
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
When Destroying, do you need the other input values of the form ?
It would be easier (and cleaner) to have two forms, one with the inputs targetting the update action and one with only the destroy button targetting the destoy action ?
If you need to access the other inputs, I would still suggest you to have two different actions and two different forms, I would additionally suggest you to use JS / jQuery to fetch the values of the fields you need from the first form when submitting the second form
链接地址: http://www.djcxy.com/p/67772.html上一篇: 动画同步,光标和高光