【问题标题】:Rails: User info not updating (Rendered ActiveModel Serializer Null with Hash)Rails:用户信息未更新(使用哈希渲染 ActiveModel Serializer Null)
【发布时间】:2018-10-10 10:38:00
【问题描述】:

相同的代码在本地运行,但在 Heroku 中不运行

def update
    current_user.update!(user_params)
    response = { message: Message.account_updated}
    json_response(response)
end
private
def user_params
    params.permit(:first_name, :last_name, :email, :password)
end

routes.rb

namespace :api do
    namespace :v1 do
        put 'user/update', to: 'users#update'
    end
end

user_serializer.rb

class UserSerializer < ActiveModel::Serializer
    attributes :id, :username, :first_name, :last_name, :email, :created_at, :updated_at
end

那是我的,它正在通过 Postman 在我的本地工作,用于更新用户信息,但是当我推送到 Heroku 并尝试通过 Postman 更新然后不起作用时,日志显示如下

2018-04-30T10:15:47.665731+00:00 heroku[router]: at=info method=PUT path="/api/v1/user/update?first_name=John&last_name=Doe" host=my-host.herokuapp.com request_id=79a84b63-be87-46f8-8fad-2d151a63722f fwd="27.147.231.22" dyno=web.1 connect=0ms service=18ms status=500 bytes=283 protocol=https
2018-04-30T10:15:47.648979+00:00 app[web.1]: [79a84b63-be87-46f8-8fad-2d151a63722f] Started PUT "/api/v1/user/update?first_name=John&last_name=Doe" 
for 27.147.231.22 at 2018-04-30 10:15:47 +0000
2018-04-30T10:15:47.650866+00:00 app[web.1]: [79a84b63-be87-46f8-8fad-2d151a63722f] Processing by Api::V1::UsersController#update as */*
2018-04-30T10:15:47.650987+00:00 app[web.1]: [79a84b63-be87-46f8-8fad-2d151a63722f]   Parameters: {"first_name"=>"John", "last_name"=>"Doe"}
2018-04-30T10:15:47.654619+00:00 app[web.1]: [79a84b63-be87-46f8-8fad-2d151a63722f]   User Load (1.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
2018-04-30T10:15:47.656446+00:00 app[web.1]: [79a84b63-be87-46f8-8fad-2d151a63722f]   CACHE User Load (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
2018-04-30T10:15:47.658345+00:00 app[web.1]: [79a84b63-be87-46f8-8fad-2d151a63722f]    (1.1ms)  BEGIN
2018-04-30T10:15:47.661446+00:00 app[web.1]: [79a84b63-be87-46f8-8fad-2d151a63722f]   User Exists (1.2ms)  SELECT  1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER($1) AND "users"."id" != $2 LIMIT $3  [["email", "j@gmail.com"], ["id", 1], ["LIMIT", 1]]
2018-04-30T10:15:47.663630+00:00 app[web.1]: [79a84b63-be87-46f8-8fad-2d151a63722f]    (1.0ms)  COMMIT
2018-04-30T10:15:47.664737+00:00 app[web.1]: [79a84b63-be87-46f8-8fad-2d151a63722f] [active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.51ms)
2018-04-30T10:15:47.665015+00:00 app[web.1]: [79a84b63-be87-46f8-8fad-2d151a63722f] Completed 500 Internal Server Error in 14ms (Views: 1.1ms | ActiveRecord: 4.5ms)

我认为这就是问题

使用哈希渲染 ActiveModel::Serializer::Null (0.51ms)

但我不明白发生了什么。

更新

module Response
    def json_response(object, status = :ok)
        render json: object, status: status
    end
end

【问题讨论】:

  • 什么是json_response 方法?它是来自某个图书馆还是你自己写的?可以贴一下代码吗?
  • @AntonTkachov 查看更新部分,将json_response
  • 尝试用序列化器指定一个特定的序列化器: UserSerializer ?它似乎没有猜到序列化程序类,因为您将代码提取到模块中。但它也会引发 500 错误,因此完整的堆栈跟踪很有用。

标签: ruby-on-rails ruby api serialization activerecord


【解决方案1】:

首先

使用哈希渲染 ActiveModel::Serializer::Null (0.51ms)

这不是不更新用户信息的问题,它会显示在日志中,因为ActiveModel::Serializer 序列化了具有null 值的列。

我认为问题出在其他地方,例如您需要重构导致此问题的原因,例如,您可以尝试删除 ! update!(user_params) 尝试像这样 current_user.update(user_params) 或其他任何方式。

# place this method inside NullAttributesRemover or directly inside serializer class
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
  hash = super
  hash.each { |key, value| hash.delete(key) if value.nil? }
  hash
end

它将序列化只有实数值的列。

我认为这会有所帮助。

【讨论】:

    猜你喜欢
    • 2015-08-05
    • 2014-01-15
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多