Rails 4 Bootstrap Search Form in Navbar

老子叫甜甜 提交于 2020-01-07 03:08:08

问题


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.



来源:https://stackoverflow.com/questions/34326453/rails-4-bootstrap-search-form-in-navbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!