Devise用户的个人资料模型?

我想扩展我的devise安装的注册forms。 我创build了一个configuration文件模型,现在问自己,我怎样才能将这个表单的特定数据添加到这个模型中。 设备的UserController在哪里?

提前致谢!

假设你有一个带有has_oneconfiguration文件关联的User模型,你只需要在User中允许嵌套属性并修改你的devise注册视图。 运行“rails generate devise:views”命令,然后使用fields_for表单助手修改devise注册#new.html.erb视图,以使您的registry单与您的用户模型一起更新您的configuration文件模型。

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

而在你的用户模式中:

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

为了补充mbreining的答案,在Rails 4.x中,您需要使用强参数来存储嵌套的属性。 创build一个注册控制器子类:

 RegistrationsController < Devise::RegistrationsController def sign_up_params devise_parameter_sanitizer.sanitize(:sign_up) params.require(:user).permit(:email, :password, profile_attributes: [:first_name, :last_name]) end end 

从你的问题来看,这不是很清楚,但我假设你的devise模型是User ,你创build了属于用户的另一个模型Profile

您需要使用rails g controller users为您的用户模型创build一个控制器。

您还需要使用rails generate devise:views views为用户rails generate devise:views以便用户在创build帐户时添加configuration文件信息。

从那里,它就像任何其他模型:创build一个用户和configuration文件实例,并将其链接。 然后,在控制器中,使用current_user.profile来访问当前用户的configuration文件。

请注意,如果您要以这种方式pipe理用户,则需要从User模型中删除:registerable模块(也请阅读https://github.com/plataformatec/devise/wiki/How-To:-Manage用户通过一个CRUD界面; )

另一种方法是为了不将视图中的构build资源放在视图中,就是重写devise控制器,确切地说,新方法只需将其更改为:

  def new build_resource({}) resource.build_profile respond_with self.resource end 

我build议查看创buildconfiguration文件,为devise用户提供一个更新的答案来解决同一个问题,并使用Rails 4 + Devise