Multiple Submit Buttons/Forms in Rails

How do I create multiple submit buttons for the same form in Rails?

You can create multiple submit buttons and provide a different value to each:

<% form_for(something) do |f| %>
..
<%= f.submit 'A' %>
<%= f.submit 'B' %>
..
<% end %>

This will output:

<input type="submit" value="A" id=".." name="commit" />
<input type="submit" value="B" id=".." name="commit" />

Inside your controller, the submitted button's value will be identified by the parameter commit. Check the value to do the required processing:

def <controller action>
if params[:commit] == 'A'
# A was pressed
elsif params[:commit] == 'B'
# B was pressed
end
end

However, remember that this tightly couples your view to the controller which may not be very desirable.

Rails: Multi-submit buttons in one Form

This was covered in Railscast episode 38. Using the params hash to detect which button was clicked is the correct approach:

View:

<%= submit_tag 'Create' %>
<%= submit_tag 'Create and Add Another', name: 'create_and_add' %>

Controller:

if params[:create_and_add]
# Redirect to new form, for example.

else
# Redirect to show the newly created record, for example.
end

Ruby on Rails, setting a different method for the same form with two submit buttons

Managed to find out what it was for those who are interested, there is an html element 'formmethod' that you can set to give the request a different method.

Here's a non-rails example from W3 schools - https://www.w3schools.com/html/html_form_attributes.asp

<form action="/action_page.php" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
<input type="submit" formmethod="post" value="Submit using POST">
</form>

Multiple submit buttons rails

I have actually managed to find what was wrong.
The problem was with:

def update_individual

@users = User.update(params[:users].keys, params[:users].values).reject { |p| p.errors.empty? }
if @users.empty?
redirect_to :back, notice: "Users updated"
else
render :action => "edit_individual"
end

end

Instead of:

redirect_to :back

It should be:

redirect_to users_path

Or any other fixed path I suppose. Is there a way to keep "redirect_to :back", or do I have to create new methods for other views (within the same controller) that will use this?

simple_form_for - multiple submit buttons with different params

I recently ran into this issue. I have two submit buttons within a simple_form_for. When I post the form to the controller the :commit params are missing.

My solution is to use jquery to capture the click event for the submits that writes the submit value to a hidden input. I can then use that input value to control logic in the controller.

Rails - Submitting multiple forms with one submit button

Against:

<%= form_for user do |f| %>

use:

<%= fields_for "users[]", user do |f| %>

After that you will get in the controller parameters values for each user_id:

"users"=>{"user_id1"=>{"attr1"=>"value1"}, "user_id2"=>{"attr1"=>"value1"}

Also to make possible updating a collection of object a way to do it is to add an action to the UsersController like this:

def update_collection
# Update users here
end

and update routing in config/routes.rb:

resources :users do
collection do
match 'update_collection', via: [:put, :patch]
end
end

and use proper url in the main form:

<%= form_tag update_collection_users_path, method: :put do |form| %>

Multiple rails forms on same page: clicking submit button per form always works in safari, sometimes works in firefox

Based on responses to comments on the OQ, it appears you have form elements wrapping your td elements inside a tr. That is invalid HTML since a form element is not a valid child of a tr, and different browsers may/do interpret markup like that differently. If you don't need IE compatibility then you can use the HTML5 form attribute to tie form elements outside a form back to the form you want them associated with, however if you do need IE compatibility you'll need to look at other solutions such as not using table/tr/td elements but possibly styling other types of elements with 'display: table/table-row/etc` or one form wrapping the whole table and determining which element to update based on the button clicked.



Related Topics



Leave a reply



Submit