gpt4 book ai didi

ruby-on-rails - 如何将 HTML5 自动对焦添加到 Rails 表单?

转载 作者:数据小太阳 更新时间:2023-10-29 07:33:56 25 4
gpt4 key购买 nike

我有一个文本框

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

产生

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

我想添加 HTML5 自动对焦,如

  = 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">

这确实有效,但我如何才能获得 autofocus 的实际 HTML 输出,如 HTML 规范所示,即

<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 的 HTML5 规范说,“这是一个 bool 值”

最佳答案

如果您愿意修补一个私有(private)的 Rails 方法,您可以获得您想要的输出。 Rails 使用 boolean_tag_option method in the TagHelper module 写出 bool 属性生成 autofocus='autofocus' 格式。

如果将以下内容添加到初始化程序中,则可以将此方法替换为以最小化格式写出属性的方法。

module ActionView::Helpers::TagHelper
private def boolean_tag_option(key)
key
end
end

显然,在升级您的应用程序所使用的 Rails 版本时,您需要格外小心。这个方法目前看来还算稳定,在4.2.0.beta.1里面还是有的和 master .

关于ruby-on-rails - 如何将 HTML5 自动对焦添加到 Rails 表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26049331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com