Rails内联表单,带有带引导程序3的form_tag

时间:2022-11-24 10:14:47

I have this form:

我有这样的形式:

<%= form_tag items_path, {method: :get, :class => "form-inline"} do %>
    <%= text_field_tag :query, params[:query] %>
    <%= submit_tag "Search", name: nil, class: "btn btn-default" %>   
<% end %>

As you can see, I'm trying to add the "form-inline" class to this form, but am doing so unsuccessfully with the present code. I've seen examples of passing html options to a form_for helper, but couldn't find out how to do it properly using form_tag and the path url generator (in my case, items_path). Help please? Thanks in advance!

正如您所看到的,我正在尝试将“form-inline”类添加到此表单中,但是使用当前代码却没有成功。我已经看到了将html选项传递给form_for帮助程序的示例,但是无法使用form_tag和路径url生成器(在我的情况下,items_path)找到正确的方法。请帮助?提前致谢!

2 个解决方案

#1


7  

Give this form_tag a try, i.e. get rid of the braces around the options.

试试这个form_tag,即摆脱选项周围的大括号。

<%= form_tag items_path, method: :get, :class => "form-inline" do %>
    <%= text_field_tag :query, params[:query] %>
    <%= submit_tag "Search", name: nil, class: "btn btn-default" %>   
<% end %>

Here is the form tag this generates:

这是生成的表单标记:

<form action="/items" class="form-inline" method="get">

#2


0  

In addition to @vee's answer, you can make better use of Bootstrap by adding more of its classes. Using HAML:

除了@ vee的答案之外,您还可以通过添加更多类来更好地使用Bootstrap。使用HAML:

= form_tag items_path, method: :get, class: 'form-inline' do
  .form-group
    = search_field_tag :query, params[:query],
      placeholder: 'Search for something',
      size: 40,
      class: 'form-control'
    = submit_tag 'Search', name: nil, class: 'btn btn-default'

Also note my use of search_field_tag instead of text_field_tag.

另请注意我使用search_field_tag而不是text_field_tag。

#1


7  

Give this form_tag a try, i.e. get rid of the braces around the options.

试试这个form_tag,即摆脱选项周围的大括号。

<%= form_tag items_path, method: :get, :class => "form-inline" do %>
    <%= text_field_tag :query, params[:query] %>
    <%= submit_tag "Search", name: nil, class: "btn btn-default" %>   
<% end %>

Here is the form tag this generates:

这是生成的表单标记:

<form action="/items" class="form-inline" method="get">

#2


0  

In addition to @vee's answer, you can make better use of Bootstrap by adding more of its classes. Using HAML:

除了@ vee的答案之外,您还可以通过添加更多类来更好地使用Bootstrap。使用HAML:

= form_tag items_path, method: :get, class: 'form-inline' do
  .form-group
    = search_field_tag :query, params[:query],
      placeholder: 'Search for something',
      size: 40,
      class: 'form-control'
    = submit_tag 'Search', name: nil, class: 'btn btn-default'

Also note my use of search_field_tag instead of text_field_tag.

另请注意我使用search_field_tag而不是text_field_tag。