有一个基于Devise系统的用户。
User_has_one:user_profile,user_profile_belongs_to:用户
User_profile_has_one:语言,language_belongs_to:User_profile
在“user_profiles”表中,有一列名为“language_id”,用于了解用户说的语言。
在“语言”表中,有一列名为“名称”。这可能是英语、西班牙语和语言种类。
现在我想将语言选择添加到我的设计的编辑页面
下面应该是这样的。正确的?
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<% resource.build_user_profile if resource.user_profile.nil? %>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<%= f.fields_for :user_profile do |profile_form| %>
<div><%= profile_form.label :nickname %><br />
<%= profile_form.text_field :nickname %></div>
<div><%= profile_form.label :language_id %><br />
<%= profile_form.collection_select(language_id, @languages, language_id, name_ja ) %></div>
<% end %>
<div><%= f.label :email %><br />
<%= f.email_field :email %></div>
<div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></div>
<br />
<div><%= f.submit "Update" %></div>
<% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete %>.</p>
<%= link_to "Back", :back %>
更新!
registrations_controller.rb 中的编辑方法
def edit
@languages = Language.all
@countries = Country.all
@prefectures = Prefecture.all
@genders = Gender.all
end