如何将HTML5自动焦点添加到rails表单?

如何将HTML5自动焦点添加到rails表单?

问题描述:

我有一个文本字段

  = text_field_tag('search_text_1', params[:search_text],
    options = {:type => 'search'} )

生成

<input id="search_text" name="search_text_1" type="search">

我想添加HTML5自动对焦,如

that I want to add HTML5 autofocus to, as in

  = text_field_tag('search_text_1', params[:search_text], 
    options = {:type => 'search', :autofocus => true} )

这会产生

<input autofocus="autofocus" id="search_text" name="search_text_1" type="search">

确实有效,但我如何才能获得自动对焦与HTML规范一样,即

which does actually work, but how I can I get the actual HTML output for the autofocus to be as the HTML spec shows, i.e.

<input autofocus id="search_text" name="search_text_1" type="search" autofocus>
# Not sure where it goes or if that matters

使用

options = {:type => 'search', :autofocus } 

给出

.../_search.html.haml:2: syntax error, unexpected '}', expecting => 
...:type => 'search', :autofocus } )

作为 https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input 说,这是一个布尔

我最终选择了

= text_field_tag('search_text_1', 
  params[:search_text], 
  options = {:type => 'search', :autofocus => true })

并接受 autofocus ='的输出自动对焦'