html - 使用 SimpleForm 在 Dropdown 中有多个选项

标签 html ruby-on-rails forms simple-form

我正在使用 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、A​​Z 等)的所有两个字母缩写

注意:我有一个以地址和州为列的位置表

最佳答案

好吧,如果你想要根据你的表格状态的动态值,你需要这个

**表格

 |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/

相关文章:

php - 文本区域输入(html 表单)未被 php 脚本回显。为什么?

html - 我们如何将 div 居中到整个页面

ruby-on-rails - Current_user 设计方法在用户 Controller 外返回 nil

html - 无法使图像相对定位

ruby-on-rails - 当我通过 Rspec 测试 Controller 时如何 stub 参数值

ruby-on-rails - articles_path 是如何工作的? (RubyOnRails 教程)

javascript - 使用 Ajax 和 PHP 的 HTML 表单无法正常工作

css - 表单字段在 Mac 上呈现不同

jQuery .attr() undefined 仅在 Chrome 中

html - 影响子文本的背景不透明度