Rails 4 Bootstrap Search Form in Navbar

2019-09-05 17:00发布

问题:

I have some rails-bootstrap-form code in a header partial that gets rendered in the Rails 4 application layout:

<div class="navbar-form navbar-right">
  <%= bootstrap_form_tag controller: 'devices', action: 'index', method: 'get' do |f| %>
    <%= f.search_field :search, hide_label: true, placeholder: 'Search' %>
    <%= f.submit "Submit", :name => nil %>
  <% end %>
</div>

I want this search form to always perform a GET request on the devices controller's index action. The search doesn't actually link to the devices index url, it appends the search parameter to the current url like this: /devices/1?search=foo. Since it just appends to the current URL, the search works fine from /devices.

I thought that if I pass in controller: 'devices', action: 'index', method: 'get' to bootstrap_form_tag it would force the URL to rewrite, but I'm not having any luck.

回答1:

I ended up stripping out bootstrap_form_tag and I went with a standard form_tag. I had to add the bootstrap form classes in manually.

<%= form_tag(devices_path, method: 'get', class: 'navbar-form navbar-right') do %>
  <%= search_field_tag 'search', nil, class: 'form-control', placeholder: 'Search'  %>
  <%= submit_tag "Submit", class: 'btn btn-default', :name => nil %>
<% end %>

I thought the bootstrap-forms gem would save time, but it definitely didn't for the search.