设计用户的配置文件模型

设计用户的配置文件模型

本文介绍了设计用户的配置文件模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展我的设备安装的注册表单.我创建了一个 Profile 模型,现在我在问自己,如何将表单的特定数据添加到这个模型中.设备的UserController在哪里?

提前致谢!

解决方案

假设您有一个带有 has_one Profile 关联的 User 模型,您只需要在 User 中允许嵌套属性并修改您的设备注册查看.

运行 rails generate devise:views 命令,然后使用 fields_for 修改设计 registrations#new.html.erb 视图,如下所示> 表单助手让您的注册表单更新您的个人资料模型以及您的用户模型.

<h1>注册</h1><%resource.build_profile%> resource_name,:url =>注册路径(资源名称))做 |f|%><%= devise_error_messages!%><h2><%= f.label :email %></h2><p><%= f.text_field :email %></p><h2><%= f.label :password %></h2><p><%= f.password_field :password%></p><h2><%= f.label :password_confirmation %></h2><p><%= f.password_field :password_confirmation %></p><%= f.fields_for :profile do |profile_form|%><h2><%= profile_form.label :first_name %></h2><p><%= profile_form.text_field :first_name %></p><h2><%= profile_form.label :last_name %></h2><p><%= profile_form.text_field :last_name %></p><%结束%><p><%= f.submit注册"%></p><br/><%=渲染:部分=>设计/共享/链接"%><%结束%>

在您的用户模型中:

class User 

I want to extend the sign up form of my devise installation. I created a Profile model and am asking myself now, how can I add specific data of the form to this model. Where is the UserController of devise located?

Thanks in advance!

解决方案

Assuming you have a User model with a has_one Profile association, you simply need to allow nested attributes in User and modify your devise registration view.

Run the rails generate devise:views command, then modify the devise registrations#new.html.erb view as shown below using the fields_for form helper to have your sign up form update your Profile model along with your User model.

<div class="register">
  <h1>Sign up</h1>

  <% resource.build_profile %>
  <%= form_for(resource, :as => resource_name,
                         :url => registration_path(resource_name)) do |f| %>
    <%= devise_error_messages! %>

    <h2><%= f.label :email %></h2>
    <p><%= f.text_field :email %></p>

    <h2><%= f.label :password %></h2>
    <p><%= f.password_field :password %></p>

    <h2><%= f.label :password_confirmation %></h2>
    <p><%= f.password_field :password_confirmation %></p>

    <%= f.fields_for :profile do |profile_form| %>
      <h2><%= profile_form.label :first_name %></h2>
      <p><%= profile_form.text_field :first_name %></p>

      <h2><%= profile_form.label :last_name %></h2>
      <p><%= profile_form.text_field :last_name %></p>
    <% end %>

    <p><%= f.submit "Sign up" %></p>

    <br/>
    <%= render :partial => "devise/shared/links" %>
  <% end %>
</div>

And in your User model:

class User < ActiveRecord::Base
  ...
  attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes
  has_one :profile
  accepts_nested_attributes_for :profile
  ...
end

这篇关于设计用户的配置文件模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:57