相同的代码在本地工作但在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
这是我和它在本地通过邮递员工作,即用于更新用户信息,但是当我推送到Heroku并尝试通过邮递员更新然后不工作时,日志显示如下
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)
我认为这就是问题
使用哈希(0.51ms)呈现ActiveModel :: Serializer :: Null
但我不明白发生了什么。
更新
module Response
def json_response(object, status = :ok)
render json: object, status: status
end
end
答案 0 :(得分:1)
首先
使用哈希(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
尝试此$concatHTML = '';
$prodList = [...]; // assume we have products list here
for ($i = 0, $max = count($prodList); $i < $max; $i++) {
$concatHTML += '<div>'. $prodList[$i] .'</div>';
}
$content = new SendGrid\Content("text/html", "
//some html content here
$concatHTML
");
或其他任何内容。
echo 'hi ' . $name
它将序列化只有实际值的列。
我认为这会有所帮助。