ruby-on-rails – 没有显示所有Devise属性的Rails 5模型对象

栏目: Ruby · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/39632699/rails-5-model-objects-not-showing-all-devise-attributes

我正在使用Rails 5 API和设计.我有一个用户模型. Schema看起来像这样.

create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.inet     "current_sign_in_ip"
    t.inet     "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token",    unique: true, using: :btree
  end

但我遇到的问题是Rails只显示:id,:email,:created_at,:updated_at属性作为模型的一部分.例如,在rails控制台中

User.new
 => #<User id: nil, email: "", created_at: nil, updated_at: nil> 

 User.first
  User Load (0.5ms)  SELECT  "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1  [["LIMIT", 1]]
 => #<User id: 2, email: "your@email.com", created_at: "2016-09-22 03:58:04", updated_at: "2016-09-22 04:54:41">

但是这些属性存在于数据库中.这个问题在前面提到过. Devise with rails 5 但那里没有答案.请帮忙.

Devise限制了像encrypted_pa​​ssword这样的属性,因此关键信息不会在API调用中暴露出来.因此,要覆盖它,您需要覆盖serializable_hash方法.

def serializable_hash(options = nil) 
  super(options).merge(encrypted_password: encrypted_password) 
end

这不是Rails 5特有的功能,而是用于保护属性的Devise功能.

希望有所帮助!

翻译自:https://stackoverflow.com/questions/39632699/rails-5-model-objects-not-showing-all-devise-attributes


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web 2.0 Architectures

Web 2.0 Architectures

Duane Nickull、Dion Hinchcliffe、James Governor / O'Reilly / 2009 / USD 34.99

The "Web 2.0" phenomena has become more pervasive than ever before. It is impacting the very fabric of our society and presents opportunities for those with knowledge. The individuals who understand t......一起来看看 《Web 2.0 Architectures》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具