设计:注册后不保存自定义字段 [英] Devise: custom fields not saved after signing up

查看:123
本文介绍了设计:注册后不保存自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

环境:Rails 4,Devise 3.2.2

鉴于我在我的设计模型中添加了两个字段(名称,专业)。
当我注册

然后我在数据库中看到新列不受影响。


以下是我的控制器和模型类

Environment: Rails 4, Devise 3.2.2
Given I added two fields (name, profession) to my devise model.
When I sign up
Then I see in the database the new columns not affected.

Below are my controller and model classes

#custom controller
class UsersController < Devise::RegistrationsController
  before_action :configure_permitted_parameters

  #devise's controller extensions

  protected

  def configure_permitted_parameters
     devise_parameter_sanitizer.for(:sign_up) do |u|
       u.permit(:name, :email, :profession, :password, :password_confirmation)
     end
  end
end

#Devise Model
class User < ActiveRecord::Base
   devise :database_authenticatable, :registerable,
          :recoverable, :rememberable, :trackable, :validatable
   attr_accessor :name, :profession
   validates :name, presence: true
   validates_uniqueness_of  :email, :name
end

我指定设计运行正确使用我的自定义控制器。

我也检查了params哈希,它包括新的字段值,你可以看到:

I specify that devise runs correctly and uses my custom controller.
I also checked in the params hash and it includes the new field values as you can see:

Processing by UsersController#create as HTML
  Parameters: {"utf8"=>"✓",  "authenticity_token"=>"F3AjMd1/EpYvuhckEMPUkiMZ1szHa6ba7OMbjjSltOk=", "user"=>{"name"=>"test", "email"=>"you@contect.com", "profession"=>"prof", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Save"}

下面是生成的sql:

And below is the generated sql:

INSERT INTO "users" ("created_at", "email", "encrypted_password", "updated_at") VALUES  (?, ?, ?, ?)  [["created_at", Tue, 11 Feb 2014 15:14:56 UTC +00:00], ["email", "you@contect.com"], ["encrypted_password", "$2a$10$Bn3LIldBUDPHKO0vrXd7peVz6q/42hOrCOcqdbvBMHVeEtn4EfKma"], ["updated_at", Tue, 11 Feb 2014 15:14:56 UTC +00:00]]

其中不包含添加的新字段。

which contains none of new fields added.

任何提示都将是w结果,我被困了一个多小时。

Any hint will be welcome, I've been stuck for more than one hour.

推荐答案

从您的型号中删除此行:

Remove this line from your model:

attr_accessor :name, :profession

这是覆盖由activer提供的访问器迁移模型并添加这些字段后记录。这导致他们不被保存到db。

This is overriding the accessors already provided by activer record after you migrated your model and added those fields. This is causing them not to be saved to the db.

这篇关于设计:注册后不保存自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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