2

我有一个用户模型和一个 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 %>
4

2 回答 2

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| %>

我认为您还需要向联系人控制器添加newcreate操作,因此表单会知道用户是否还user profile没有,它将创建新的而不是更新。

于 2012-11-10T05:57:34.330 回答
3

我终于发现,在整个过程中,我都错过了一个=.

我变了

<% f.fields_for :user_details do |d| %>

<%= f.fields_for :user_details do |d| %>

他们都在那里。

于 2012-11-13T01:25:46.397 回答