Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 
like image 604
HUSTEN Avatar asked Dec 21 '12 09:12

HUSTEN


1 Answers

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

like image 128
shweta Avatar answered Sep 27 '22 23:09

shweta