How to Create Multiple Submit Buttons For the Same Form 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?

Correct way to have multiple submit buttons on single form in rails

This is a vast subject, but for an ajax delete button you can start like this. In your table view, use :

<tr id="tr_<%= item.id %>">
...
<%= link_to 'delete', item, method: :delete, remote: true %>
</tr>

You must handle the ajax call on destroy route in your items_controller.rb:

def destroy
@item= Item.destroy(params[:id])
respond_to do |format|
format.html { redirect_to items_url }
format.js
end
end

Create a new destroy.js.erb file in your items views folder and put in it :

$('#your_table').remove('#tr_<%= @item.id %>');

Then when you click on the remove button, it will call the destroy action in your controller. As the button is defined as remote, rails will automagically perform an ajax request and won't reload the page. Your controller will render the according js view in which you then remove the row from your table.

As I said, this is only the beginning and it can be done much better on the javascript side. Also note that I didn't check the code.

Also have a look at http://railscasts.com/episodes/136-jquery-ajax-revised?view=asciicast for a more in depth explanation.

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: multiple submit button outside simple form

I resolved it by putting in our html form template:

<input type="hidden" name= "action_type" id="action_type">

then in JavaScript file we added

$('.x-update').on 'click', ->
$("#action_type").val("exit")
$("#details-form").submit()

and then in controller file we checked the parameters:

if params[:action_type] == "exit"
redirect_to #your desired path
else
redirect_to #based on given path

That's It. It works.



Related Topics



Leave a reply



Submit