我有一个用户模型和一个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
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 %>
答案 0 :(得分:4)
您使用了错误的关联名称:
has_one :user_detail
,而不是has_one :user_details,
@userProfile.build_user_detail
,而不是@userProfile.build_user_details
编辑和更新操作时,您的http方法为put
,而不是post
,因此请更改此信息:
<%= form_for(@userProfile, :url => {:controller => "my_devise/contacts", :action => "update_user"}, :html => {:class => "form grid_6"}, :method => :post ) do |f| %>
对此:
<%= form_for(@userProfile, :url => {:controller => "contacts", :action => "update_user"}, :html => {:class => "form grid_6"}, :method => :put ) do |f| %>
尝试重新编辑和更新。
但我更喜欢将update_user
操作重命名为update
,它会遵循rails的惯例,制作适合您的编辑和操作。更新更容易。如果您要编辑和更新联系人的路线如下:
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
contact PUT /contacts/:id(.:format) contacts#update
您只需要定义表单:
<%= form_for(@userProfile, :html => {:class => "form grid_6"}) do |f| %>
我认为您还需要向联系人控制器添加new
和create
操作,因此表单会知道用户是否还没有user profile
,它会创建新的而不是更新。< / p>
答案 1 :(得分:3)
我终于想通了整个时间,我错过了=
。
我改变了
<% f.fields_for :user_details do |d| %>
到
<%= f.fields_for :user_details do |d| %>
他们都在那里。