skw*_*eth 6 forms ruby-on-rails
我的 Rails 表单中有一个选择输入 -
<%= f.select :store_type_id, @storeType.map{ |type| [type.store_type.capitalize, type.id] }, {include_blank: 'Select store type'} %>
Run Code Online (Sandbox Code Playgroud)
我怎样才能使第一个选项(“选择商店类型”)既被禁用又被选择?
我想要的 html 等效项是
<option disabled selected>Select store type</option>
Run Code Online (Sandbox Code Playgroud)
谢谢你!
没有办法(本机)执行此操作来确保跨浏览器功能,但这是一个答案:
# on store types
def self.options
options = self.all.collect { |type| [type.capitalize, type.id] }
options.unshift(["Select Store Type", "0"])
options
end
# in your form
<%= f.select :store_type_id, options_for_select(StoreType.options, :disabled => "0") %>
Run Code Online (Sandbox Code Playgroud)
但是,您依赖于浏览器来选择禁用的输入,这将是自相矛盾的。Chrome 最新版会转到下一个非禁用输入。
相反,您可能需要进行一些表单验证以确保选择的值不为空。
为了让未来的 Ruby 开发人员感到高兴(并让自己遵守约定和最佳实践),请记住 Ruby 支持对变量进行 Snake_casing :)
| 归档时间: |
|
| 查看次数: |
6172 次 |
| 最近记录: |