加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 为Devise用户创建配置文件

发布时间:2020-12-16 20:51:48 所属栏目:百科 来源:网络整理
导读:所以我采取了 Profile model for Devise users?中描述的步骤 我正在使用rails 4和ruby 1.9.3p448 class User ActiveRecord::Basedevise :database_authenticatable,:registerable,:recoverable,:rememberable,:trackable,:validatable attr_accessible :emai
所以我采取了 Profile model for Devise users?中描述的步骤
我正在使用rails 4和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" %>

#

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

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控制器中做了什么?
提前致谢

解决方法

我找到了解决方案!
在我的users / registrations_controller.rb中,我不得不添加以下内容
before_filter :configure_permitted_parameters,if: :devise_controller?

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

它工作得很好!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读