Navbar中的Rails 4 Bootstrap搜索表单 [英] Rails 4 Bootstrap Search Form in Navbar
问题描述
我在标头部分中有一些 rails-bootstrap-form 代码,在Rails 4应用程序布局中呈现:
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>
我希望此搜索表单始终对设备控制器的索引操作执行GET
请求.搜索实际上并没有链接到设备索引URL,而是将搜索参数附加到当前URL,如下所示:/devices/1?search=foo
.由于它只是附加到当前URL,因此可以从/devices
正常搜索.
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
.
我认为,如果将controller: 'devices', action: 'index', method: 'get'
传递给bootstrap_form_tag
,将迫使URL进行重写,但是我没有任何运气.
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.
推荐答案
我最终剥离了bootstrap_form_tag
,然后选择了标准的form_tag
.我必须手动添加引导表单类.
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 %>
我认为引导形式的gem可以节省时间,但是绝对不是搜索的目的.
I thought the bootstrap-forms gem would save time, but it definitely didn't for the search.
这篇关于Navbar中的Rails 4 Bootstrap搜索表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!