Ruby 在 Rails - 表单 Select 下拉菜单 Selected 编辑选项
Ruby On Rails - Forms For Select Dropdown Selected option on Edit
好的,所以我正在使用 Rails 5 和 Ruby 的最新 stable 版本。我想要做的是让一个 select 框在编辑视图上有正确的选项 selected。
这是我目前所拥有的
create_users
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :email
t.string :password
t.integer :user_type_id
t.timestamps
end
end
end
class CreateUserTypes < ActiveRecord::Migration[5.0]
def change
create_table :user_types do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
这些table之间没有任何关系,所有UserType只是一个支持table。
我可以让它输出下拉列表并保存到数据库我只是无法在编辑时显示适当的 selected 选项。
这是我的表单代码
<%= form_for(user) do |f| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.text_field :password %>
</div>
<div class="field">
<%= f.label :user_type_id %>
<%#= select_tag('user_type', options_for_select(UserType.all.collect {|ut| ut.name ut.id})) %>
<% user_type_array = UserType.all().map { |type| [type.name, type.id]} %>
<%= f.select(:user_type_id, options_for_select(user_type_array), :selected => f.object.user_type_id) %>
<%#= options_from_collection_for_select(UserType.all(), :id, :name) #just outputs text %>
<%#= f.select_tag('user_type_id', options_from_collection_for_select(UserType.all(), :id, :name)) %>
</div>
<%= params.inspect %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
使用options_for_select中的值,它需要一个可选的selected
参数:
options_for_select(user_type_array, f.object.user_type_id)
好的,所以我正在使用 Rails 5 和 Ruby 的最新 stable 版本。我想要做的是让一个 select 框在编辑视图上有正确的选项 selected。
这是我目前所拥有的 create_users
class CreateUsers < ActiveRecord::Migration[5.0]
def change
create_table :users do |t|
t.string :email
t.string :password
t.integer :user_type_id
t.timestamps
end
end
end
class CreateUserTypes < ActiveRecord::Migration[5.0]
def change
create_table :user_types do |t|
t.string :name
t.text :description
t.timestamps
end
end
end
这些table之间没有任何关系,所有UserType只是一个支持table。 我可以让它输出下拉列表并保存到数据库我只是无法在编辑时显示适当的 selected 选项。
这是我的表单代码
<%= form_for(user) do |f| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %>
<%= f.text_field :password %>
</div>
<div class="field">
<%= f.label :user_type_id %>
<%#= select_tag('user_type', options_for_select(UserType.all.collect {|ut| ut.name ut.id})) %>
<% user_type_array = UserType.all().map { |type| [type.name, type.id]} %>
<%= f.select(:user_type_id, options_for_select(user_type_array), :selected => f.object.user_type_id) %>
<%#= options_from_collection_for_select(UserType.all(), :id, :name) #just outputs text %>
<%#= f.select_tag('user_type_id', options_from_collection_for_select(UserType.all(), :id, :name)) %>
</div>
<%= params.inspect %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
使用options_for_select中的值,它需要一个可选的selected
参数:
options_for_select(user_type_array, f.object.user_type_id)