简介模型设计的用户? [英] Profile model for Devise users?
问题描述
我想我用推广安装色器件的注册表单。我创建了一个档案模型,并问自己,现在,我怎么能形式的特定数据添加到这个模型。哪里是色器件由于UserController位于?
i want to extened the sign up form of my devise installation. i created a Profile model and asking myself now, how can i add specific data of the form to this model. Where is the UserController of devise located?
在此先感谢!
推荐答案
假设你有一个HAS_ONE个人资料相关联的用户模式,您只需允许用户嵌套的属性和修改色器件的注册视图。运行的导轨上产生设计:观点'。然后命令修改色器件注册#new.html.erb视图如下图所示使用fields_for形式帮助有你的注册表格与您的用户模型一起更新您的个人资料型号
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
这篇关于简介模型设计的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!