Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: User info not updating (Rendered ActiveModel Serializer Null with Hash)

The same code is working in the local but not working in 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

that is my and it's working on my local via Postman, that is for updating user info, but while I push to Heroku and try to update via Postman then not working, log showing like this

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", "[email protected]"], ["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)

I think that's the problem

Rendered ActiveModel::Serializer::Null with Hash (0.51ms)

But I don't understand what's going on.

Update

module Response
    def json_response(object, status = :ok)
        render json: object, status: status
    end
end
like image 515
jesica Avatar asked Oct 12 '25 04:10

jesica


1 Answers

First of all

Rendered ActiveModel::Serializer::Null with Hash (0.51ms)

That's not the issue for not updating user info, it's showing in the log because ActiveModel::Serializer serialize the columns which have null value.

I think the problem is elsewhere like you need to refactor why causes this e.g you can try to remove ! update!(user_params) try like this current_user.update(user_params) or any other.

# 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

it will serialize columns which have only real values.

I think it will help.

like image 94
fool-dev Avatar answered Oct 14 '25 19:10

fool-dev