我正在使用 Ruby on Rails 并尝试添加仅包含指定选项的下拉表单,这些选项是状态。
到目前为止,我有以下内容:
<%= simple_form_for @location do |f| %>
<%= f.input :address %>
<%= f.input :state %>
<%= f.button :submit, "Add Location" %>
<% end %>
但是,我想添加类似的东西
<%= f.input :state %>
但只允许美国所有州(AL、AK、AZ 等)的所有两个字母缩写
注意:我有一个以地址和州为列的位置表
最佳答案
好吧,如果你想要根据你的表格状态的动态值,你需要这个
**表格
|states|
|id| |name|
1 ALASKA
2 AK
3 AZ
**在你的 Controller 中:
@states: State.all
**在您看来:
States:
<%= f.select "states",options_for_select(@states.collect {|t| [t.name.to_s ,t.id]}, params[:states].to_i ) %>
如果你想要静态值,你应该尝试:
<%= f.input :state, collection: ['AL', 'AK', 'AZ'] %>
关于html - 使用 SimpleForm 在 Dropdown 中有多个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22871055/