I had a model that should be rendered as JSON, for that I used a serializer
class UserSerializer
def initialize(user)
@user=user
end
def to_serialized_json
options ={
only: [:username, :id]
}
@user.to_json(options)
end
end
when I render json:
I want though to add a JWT token and an :errors
. Unfortunately I am having an hard time to understand how to add attributes to the serializer above. The following code doesn't work:
def create
@user = User.create(params.permit(:username, :password))
@token = encode_token(user_id: @user.id) if @user
render json: UserSerializer.new(@user).to_serialized_json, token: @token, errors: @user.errors.messages
end
this code only renders => "{\"id\":null,\"username\":\"\"}"
, how can I add the attributes token:
and errors:
so to render something like this but still using the serializer:
{\"id\":\"1\",\"username\":\"name\", \"token\":\"eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoxfQ.7NrXg388OF4nBKLWgg2tdQHsr3HaIeZoXYPisTTk-48\", \"errors\":{}}
I coudl use
render json: {username: @user.username, id: @user.id, token: @token, errors: @user.errors.messages}
but how to obtain teh same by using the serializer?
each time you want to add new more fields to be serialized, you can do something like
UserSerializer.new(@user).to_serialized_json(:token, :errors)
if left empty, it will use the default field
:id, :username
if you want the json added to be customizable
UserSerializer.new(@user).to_serialized_json(token: @token, errors: @user.error.messages)
if left empty, it will still behaves like the original class you posted