我有一个用户模型和一个 user_details 模型。User_details 仅包含有关用户的更多详细信息。
我正在尝试创建一个页面,其中有人可以在一个页面上编辑用户和该用户的 user_details,但由于我的 users 表中有 1 行,而我的 user_details 表中没有行,所以没有文本字段显示在我的编辑页面。
如果 user_details 表中不存在数据,如何让 user_details 的文本字段显示在我的编辑页面上?
我的contacts_controller的一部分:
# GET /contacts/1/edit
# shows a users profile in edit mode
def edit
@userProfile = User.find(params[:id])
@userProfile.build_user_details
#@userProfile.user_details.build
#question.answers.build
respond_to do |format|
format.html
end
end
# POST /contacts/1/edit
# actually updates the users data
def update_user
@userProfile = User.find(params[:id])
respond_to do |format|
if @userProfile.update_attributes(params[:user])
format.html {
flash[:success] = "Information updated successfully"
render :edit
}
else
format.html {
flash[:error] = resource.errors.full_messages
render :edit
}
end
end
end
用户模型
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :authentication_keys => [:login]
# Virtual attribute for authenticating by either username or email
# This is in addition to a real persisted field like 'username'
attr_accessor :login
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :login, :first_name, :last_name, :home_phone, :cell_phone, :work_phone, :birthday, :home_address, :work_address, :position, :company, :user_details
has_one :user_details, :dependent => :destroy
accepts_nested_attributes_for :user_details
# validates email or username when logging in
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
else
where(conditions).first
end
end
end
User_details 模型
class UserDetails < ActiveRecord::Base
belongs_to :user
end
编辑.html.erb
<%= form_for(@userProfile, :url => {:controller => "my_devise/contacts", :action => "update_user"}, :html => {:class => "form grid_6"}, :method => :post ) do |f| %>
<fieldset>
<legend>Update profile information</legend>
<%= f.label :first_name, "First Name" %>
<%= f.text_field :first_name, :required => "required" %>
<%= f.label :last_name, "Last Name" %>
<%= f.text_field :last_name, :required => "required" %>
<%= f.label :username, "Username" %>
<%= f.text_field :username, :required => "required" %>
<% f.fields_for :user_details do |d| %>
<%= d.label :home_phone, "Home Phone" %>
<%= d.text_field :home_phone %>
<%= d.label :cell_phone, "Cell Phone" %>
<%= d.text_field :cell_phone, :required => "required" %>
<%= d.label :work_phone, "Work Phone" %>
<%= d.text_field :work_phone %>
<%= d.label :birthday, "Birthday" %>
<%= d.text_field :birthday %>
<%= f.label :email, "Email" %>
<%= f.text_field :email, :required => "required" %>
<%= d.label :home_address, "Home Address" %>
<%= d.text_field :home_address, :required => "required" %>
<%= d.label :work_address, "Work Address" %>
<%= d.text_field :work_address %>
<%= d.label :position, "Position" %>
<%= d.text_field :position %>
<%= d.label :company, "Company" %>
<%= d.text_field :company, :required => "required" %>
<% end %>
<div class="action">
<%= f.submit "OK", :class => "button button-orange" %>
<button class="button button-gray" type="reset">Reset</button>
</div>
</fieldset>
<% end %>