我在轨道上使用红宝石。尝试设置一个下拉菜单,供用户选择他们来自哪个州。是否可以将州作为选项显示,但将其选择记录为state_id?
.erb文件
$additional_parameters
州名表的数据库架构
<% provide(:title, 'New Profile') %>
<%= bootstrap_form_for(@profile) do |f| %>
<%= f.text_field :name, :autofocus => true, :placeholder => 'Name' %>
<%= f.number_field :age, :placeholder => 'Age' %>
<%= f.form_group :sex, label: { text: "Sex" } do %>
<br>
<%= f.radio_button :sex, 'Male', label: "Male", inline: true %>
<%= f.radio_button :sex, 'Female', label: "Female", inline: true %>
<% end %>
<%= f.text_field :city, :id => 'gmaps-input-address', :placeholder => "City" %>
<%= f.select :state_id, options_for_select(State.pluck(:home_state)), :label => "State", :class => "dropdown-menu" %>
<%= f.submit "Submit", :class =>'btn btn-primary' %>
<% end %>
配置文件表的架构
create_table "states", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "home_state"
t.string "code"
end
答案 0 :(得分:1)
这绝对是可能的。你很接近,但你需要像这样格式化你的表单助手来获得所需的结果:
<%= f.select(:state_id, options_for_select(State.all.collect {|p| [ p.home_state, p.id ]), :label => "State", :class => "dropdown-menu")%>
此处有更多文档:http://apidock.com/rails/ActionView/Helpers/FormOptionsHelper/select