我创建了一个迁移,该迁移向我们的表/模型(cloud_photo_url
)中添加了新列。但是,当我尝试将其渲染时,API不会生成该值。我还注意到其他一些属性也丢失了。
我不知道如何获取json输出以包含来自cloud_photo_url
的值。需要明确的是,它被保留在数据库中,但是由于任何原因,API均未返回该值。如果我更改或更新其他值(例如first_name
或last_name
),这些更改将反映在json响应中。
db /模式:
Column | Type | Modifiers
-----------------+-----------------------------+-------------------------------------------------------
id | uuid | not null default shared_extensions.uuid_generate_v4()
last_seen_at | timestamp without time zone |
auth_token | character varying |
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
first_name | character varying |
last_name | character varying |
email | character varying |
birthday | date |
provider_id | character varying |
password_digest | character varying |
gender | integer | default 0
verified | boolean | default false
photo | character varying |
provider | integer | default 0
cloud_photo_url | character varying |
Ruby代码:
@user = User.find(params[:id])
render json: @user
api端点的json输出:
{
"user": {
"id": "8c29a48e-c2c2-4374-b8e6-f9d7c6970a9a",
"auth_token": "WH4fveVNa1vANgD6LKbVJPL2",
"provider": "guest",
"provider_id": "111959522860722",
"first_name": "miguel",
"last_name": "martin",
"email": "a@b.com",
"verified": false,
"gender": "unknown",
"birthday": null,
"photo": null,
"pois": []
}
}
•根据注释,puts @user.to_json
确实可以处理全部数据库内容。
•是的,我确定这些值将持久保存到数据库中。我也确定路线正确(如我在问题中所指出的,如果我更新显示的其他值,它们在响应中也会类似地更新)。
•我同意似乎有一些干扰render :json
的问题,但是我无法弄清楚是什么。我寻找了任何.rabl
文件,但未找到任何文件。
•使用render inline: @user.to_json, content_type: "application/json"
使我非常接近... json只是略有畸形(封装不完美)...但是我更喜欢以“正确的方式”进行修复。
有关如何配置可能影响此路线/渲染/模型/控制器的任何其他帮助将很有帮助。
有人问我的模型,这里是:
class User < ApplicationRecord
attr_accessor :updating_password
mount_uploader :photo, UserPhotoUploader
has_secure_password validations: false
has_secure_token :auth_token
enum gender: { unknown: 0, female: 1, male: 2 }, _prefix: true
enum platform: { ios: 0, android: 1 }
enum provider: { guest: 0, email: 1, facebook: 2 }, _prefix: true
has_many :comments, dependent: :destroy
has_many :enters, dependent: :destroy
has_many :pois, through: :subscriptions
has_many :subscriptions, dependent: :destroy
has_many :devices, dependent: :destroy
has_many :outgoing_messages, class_name: 'MessageQueue', dependent: :destroy
has_many :user_messages, dependent: :destroy
has_many :messages, through: :user_messages, source: :user
validates_associated :devices
validates :provider_id, presence: true, if: :provider_requires_id
validates :first_name, presence: true, unless: :provider_is_guest
validates :last_name, presence: true, unless: :provider_is_guest
validates :email,
presence: true,
uniqueness: { case_sensitive: false },
format: { with: REGEX_EMAIL, message: 'Invalid email address' },
unless: :provider_is_guest
validates :password, length: { minimum: 6, maximum: 128, message: 'must be greater than 5 characters' }, if: :setting_password?
validates :provider_id, uniqueness: {
scope: [:provider],
message: 'Provider ID already exists'
}, if: :provider_requires_id
def gender=(g)
g.try(:downcase!)
gender = g.blank? ? 0 : g
super(gender)
end
# IF not guest or email account then it requires a provider ID
def provider_requires_id
!['guest', 'email'].include?(self.provider)
end
def provider_is_guest
provider_guest?
end
def setting_password?
(self.provider_email? && new_record?) || updating_password
end
def name
provider_guest? ? "Guest User" : "#{first_name} #{last_name}"
end
def send_email_verification
UserVerificationMailer.perform_later(current_tenant, self)
end
def send_password_reset
UserPasswordResetMailer.perform_later(current_tenant, self)
end
end
答案 0 :(得分:1)
让我们说
@user = User.find(params[:id])
render json: @user
这意味着它将直接查找UserSerializer
来呈现@user
对象。
UserSerializer
中定义的属性将呈现为json输出。
除此active_model_serializers
之外,还有一些自定义json输出的选项,例如:-
render json: @user, serializer: CustomUserSerializer, serializer_options: {}, status: 200
在custom_user_serializer.rb
class CustomUserSerializer < ActiveModel::Serializer
attributes :id,
:last_seen_at,
:auth_token,
:created_at,
:updated_at
:...#so on
end