Reputation:
I have a button:
<%= t.submit "#{ t('page.upload_image')}", :name => "image" %>
Can I get the name of button("image") from the controller?
Upvotes: 1
Views: 972
Reputation: 1098
You can't get html attributes in rails params. You need to send it in hidden_field which will be accessible in controller.
<%= hidden_field_tag 'name', 'image'%>
Or you have multiple buttons, then you can write jquery on button click, assign button name to hidden_field and then submit form.
Jquery:
$('.submit_button').click(function(){
var name = $(this).attr('name')
$('#name_hidden_field').val(name);
$(form).submit();
})
HTML:
<%= hidden_field_tag 'name', '', :id => 'name_hidden_field' %>
<%= button_tag "#{ t('page.upload_image')}", :class=> "submit_button", :name => 'image' %>
<%= button_tag "#{ t('page.upload_image')}", :class=> "submit_button", :name => 'image2' %>
In both cases, you'll get name in params[:name]
form.
Upvotes: 0
Reputation: 7612
Well this is how i do it:
<%= submit_tag "Estimate recordcount", :name => 'count' %>
<%= submit_tag "Download CSV", :name => 'download' %>
Then in the controller i check:
if params[:download].present?
end
Upvotes: 1