Итак, я использую Rails 5 и самую последнюю стабильную сборку Ruby. То, что я пытаюсь сделать, - это выбрать поле выбора, выбранное в окне редактирования.Ruby On Rails - формы для выбора раскрывающегося списка Выбранная опция в редакторе
Это то, что я до сих пор 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
Там не существует никаких отношений между этими таблицами, все UserType просто таблица поддержки. Я могу получить его для вывода выпадающего списка и сохранения в базу данных. Я просто не могу заставить чертово отображать соответствующий выбранный параметр при редактировании.
Вот мой код формы
<%= 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 %>
Совершенная спасибо! Работал как шарм, forms_for может быть немного запутанным время от времени! Еще раз спасибо! –