为 Devise 用户创建配置文件 [英] Creating Profile for Devise users

查看:34
本文介绍了为 Devise 用户创建配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我采取了Devise 用户的配置文件模型?中描述的步骤我使用的是 rails 4 和 ruby​​ 1.9.3p448

So I took the steps described in Profile model for Devise users? I'm using rails 4 and ruby 1.9.3p448

class User < ActiveRecord::Base

devise :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable 

attr_accessible :email, :password, :password_confirmation, :remember_me, :profile_attributes
has_one :profile
accepts_nested_attributes_for :profile       


protected

def profile
  super || build_profile
end 

end

#

class Profile < ActiveRecord::Base  
    belongs_to :user
    attr_accessible :uname, :manager
end

#

<h2>Sign up...</h2>
<%= form_for resource, :as => resource_name, :url => registration_path(resource_name) do |f| %>

<%= devise_error_messages! %>

<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %><br />
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>  

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

<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "devise/shared/links" %>

#

仍然无法为用户保存配置文件,这是输出:

#

Still can't save profile for user and this is the output:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"kG7S9lF4+5hm+ggmKA4LZyXrN4hsPf01jGQvKxgzGGI=", "user"=>{"email"=>"test1@test.test", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "profile_attributes"=>{"uname"=>"1", "manager"=>"1"}}, "commit"=>"Sign up"} **Unpermitted parameters: profile_attributes**

我应该在 Profiles 控制器中做些什么吗?提前致谢

Shoud I do anything in Profiles controller? Thanks in advance

推荐答案

我找到了解决方案!在我的 users/registrations_controller.rb 中,我必须添加以下内容

I found the solution! In my users/registrations_controller.rb, I had to add the following

 before_filter :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
   devise_parameter_sanitizer.for(:sign_up) {|u| 
     u.permit(:email, :password, :password_confirmation, :remember_me, 
     profile_attributes: [:uname, :manager])}
end

而且效果很好!

这篇关于为 Devise 用户创建配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆