ユーザー モデルと user_details モデルがあります。User_details には、ユーザーに関する詳細が含まれているだけです。
ユーザーとそのユーザーの user_details をすべて 1 つのページで編集できるページを作成しようとしていますが、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
edit.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 %>