我想扩展我的devise安装的注册表单。我创建了一个配置文件模型,现在我问自己,如何才能将表单的特定数据添加到此模型中。devise的UserController在哪里?
提前感谢!
发布于 2010-12-01 03:16:12
假设您有一个具有has_one配置文件关联的用户模型,您只需要在User中允许嵌套属性并修改您的devise注册视图。
运行rails generate devise:views命令,然后使用fields_for表单助手修改devise registrations#new.html.erb视图,如下所示,让您的注册表单随用户模型一起更新您的配置文件模型。
<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发布于 2014-01-08 01:11:36
为了补充mbreining的答案,在Rails4.x中,您需要使用strong parameters来允许存储嵌套属性。创建一个注册控制器子类:
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发布于 2010-11-30 04:45:39
你的问题不是很清楚,但我假设你的设计模型是User,并且你创建了另一个属于用户的模型Profile。
您需要使用rails g controller users为您的用户模型创建一个控制器。
您还需要使用rails generate devise:views为您的用户生成视图,以便用户在创建帐户时可以添加配置文件信息。
从那里开始,它就像任何其他模型一样:创建一个user和profile实例,并将两者链接起来。然后,在控制器中,使用current_user.profile访问当前用户的配置文件。
请注意,如果您打算以这种方式管理用户,则需要从User模型中删除:registerable模块(也请阅读https://github.com/plataformatec/devise/wiki/How-To:-Manage-users-through-a-CRUD-interface)
https://stackoverflow.com/questions/4307743
复制相似问题