Rails 5模型对象不显示所有属性 [英] Rails 5 model objects not showing all attributes

查看:102
本文介绍了Rails 5模型对象不显示所有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Rails 5 API与设计。我有一个用户模型。模式看起来像这样。

  create_tableusers,force::cascade do | t | 
t.stringemail,默认值为,null:false
t.stringencrypted_pa​​ssword,默认值为,null:false
t.stringreset_password_token
t.datetimereset_password_sent_at
t.datetimeremember_created_at
t.integersign_in_count,默认值:0,null:false
t.datetimecurrent_sign_in_at
t.datetimelast_sign_in_at
t.inetcurrent_sign_in_ip
t.inetlast_sign_in_ip
t.datetimecreated_at,null:false
t.datetimeupdated_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唯一:true,使用::btree
end

但是我遇到的问题是Rails只显示:id,:email,:created_at,update updated = attri butes作为模型的一部分。例如,在rails控制台中

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

User.first
用户加载(0.5ms)SELECTusers。* FROMusersORDER BYusers。idASC 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>

但这些属性存在于数据库中。 使用rails 5设计。但是没有答案。请帮助。

解决方案

Devise限制属性,如 encrypted_pa​​ssword 以使关键信息不会在API调用中暴露出来。所以要重写这个,你需要覆盖 serializable_hash 方法。

  def serializable_hash(options = nil)
super(options).merge(encrypted_pa​​ssword:encrypted_pa​​ssword)
end

这不是Rails 5的特定功能,而是一个Devise功能来保护您的属性。



希望有帮助!


I am using Rails 5 API with devise. I have a User model. Schema looks like this.

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

But the issue I am having is Rails only showing :id , :email , :created_at , :updated_at attributes as part of model. For example, in rails console

 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"> 

But these attributes exist in database.This problem is mentioned earlier . Devise with rails 5 . But no answers there .Please help.

解决方案

Devise restricts attributes like encrypted_password so that the critical information doesn't get exposed in API calls. So to override this, you need to override serializable_hash method.

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

This is not a Rails 5 specific feature but a Devise feature to protect your attributes.

Hope that helps!

这篇关于Rails 5模型对象不显示所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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