0

I have a search form in the header navbar so that it is always available to users, but I can't get it to send to the correct controller/action. It works correctly when already on the page for search results, when used from any other page it sends to the controller/action for the current page . I found a similar problem here Rails 4 Bootstrap Search Form in Navbar but I did not run into the issue in the same fashion, nor did it help me. Where am I going wrong?

The form in my header navbar

<ul class="navbar-form navbar-left">
  <form class="form-inline">
    <%= form_tag(search_path, method: :get) do %>
      <%=select_tag :gender, options_for_select
        ([['Male & Female', 'All'],['Male', 'Male'], 
        ['Female', 'Female']]) %>
      <%= label_tag "Include unavailable" %>  
      <%= check_box_tag :unavailable %>
      <%= submit_tag "Search", class: "btn btn-success navbar-btn" %>
    <% end %>
  </form>
</ul>

Routing

get  '/search', to: 'searches#search'

I have tried building the form both as form_for and form_tag, as well as URL options with either to force the correct controller/action, to no avail. It only appends the form params to the current url.

What am I missing?

Tristin
  • 69
  • 8

1 Answers1

0

I think I see the issue now.

  <form class="form-inline">
    <%= form_tag(search_path, method: :get) do %>

is actually putting two form tags on your page. You want to get rid of the outer <form> tag and add a class: 'form-inline' to your form_tag call.

You'll want a name on that route too:

get  '/search', to: 'searches#search', as: 'search'
nc.
  • 7,179
  • 5
  • 28
  • 38