7

How do I pass a url on the form_for submit? I'm trying to use one form with each buttons pointing to each controller actions, one is search and another one is create. Is it possible to have 2 submit buttons with different actions on the same form?

<%= form_for @people do |f| %>
    <%= f.label :first_name %>:
    <%= f.text_field :first_name %><br />

    <%= f.label :last_name %>:
    <%= f.text_field :last_name %><br />

    <%= f.submit(:url => '/people/search') %>
    <%= f.submit(:url => '/people/create') %>
<% end %>
JB Juliano
  • 632
  • 1
  • 9
  • 20

3 Answers3

21

There is not a simple Rails way to submit a form to different URLs depending on the button pressed. You could use javascript on each button to submit to different URLs, but the more common way to handle this situation is to allow the form to submit to one URL and detect which button was pressed in the controller action.

For the second approach with a submit buttons like this:

<%= form_for @people do |f| %>
  # form fields here
  <%= submit_tag "First Button", :name => 'first_button' %>
  <%= submit_tag "Second Button", :name => 'second_button' %>
<% end %>

Your action would look something like this:

def create
  if params[:first_button]
    # Do stuff for first button submit
  elsif params[:second_button]
    # Do stuff for second button submit
  end
end

See this similar question for more about both approaches.

Also, see Railscast episode 38 on multibutton forms.

Community
  • 1
  • 1
Troy
  • 5,319
  • 1
  • 35
  • 41
  • 1
    Agree, keep things simple, use native approach, **js** can manage *action url*. I'd suggest you to use another form for searching with adjusted CSS to reposition – Anatoly Aug 13 '11 at 06:58
2

This question is very similar to this one, though it's a bit different

I just wanted to higlight that some answer to the aforementionned question also suggested to add constraints to the routes, so you can actually route the queries to different controller actions !

Credits to the author, vss123

We solved using advanced constraints in rails.

The idea is to have the same path (and hence the same named route & action) but with constraints routing to different actions.

resources :plan do   
  post :save, constraints: CommitParamRouting.new("Propose"), action: :propose
  post :save, constraints: CommitParamRouting.new("Finalize"), action: :finalize 
end

CommitParamRouting is a simple class that has a method matches? which returns true if the commit param matches the given instance attr. value.

This available as a gem commit_param_matching.

Community
  • 1
  • 1
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
  • Note that unlike JS-based answers that replace the form action url, this method relies entirely on the backend and will work even for clients that have disabled javascript – Cyril Duchon-Doris Feb 04 '17 at 10:17
0

I'm facing the exact same problem.

I have a create form. However, if the record already exists, I want to show the results via a search results.

Option 1: Have two separate forms

Have two separate forms:

<%= form_for @people do |f| %>
    <%= f.submit(:url => '/people/search') %>    
<% end %>

When you submit the above form, write some javascript to also submit the form below.

<%= form_for @people do |f| %>
    <%= f.submit(:url => '/people/search') %>    
<% end %>

Option 2: Have one form with two button

  • Like Cyril Duchon-Doris says.
  • Hide the search button. Use a stimulus controller action to trigger submissions via the search button.
  • Turbo stream the results.
BenKoshy
  • 33,477
  • 14
  • 111
  • 80