Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding render_create_success does not obey serializer option (AMS) #890

Closed
cireficc opened this issue May 16, 2017 · 2 comments
Closed

Comments

@cireficc
Copy link

I wanted to override the JSON sent after successful sign in, so I did this:

# config/routes.rb
mount_devise_token_auth_for 'User', at: 'auth', controllers: { sessions: "sessions" }
# app/controllers/sessions_controller.rb
class SessionsController < DeviseTokenAuth::SessionsController
  def render_create_success
    render json: current_api_user, serializer: Api::V2::UserSerializer
  end
end

But the method did not respect the serializer option; it seemed to call as_json directly on the model object (unless I'm mistaken). However, building the json separately before calling render actually uses the active_model_serializers UserSerializer:

# app/controllers/sessions_controller.rb
class SessionsController < DeviseTokenAuth::SessionsController
  def render_create_success
    data = Api::V2::UserSerializer.new(current_api_user).as_json
    render json: data
  end
end

Is this a bug?

@dkniffin
Copy link
Contributor

I ran into a similar issue. Here's my workaround:

  def render_update_success
    render json: {
      status: "success",
      data: resource_data(
        resource_json: ActiveModel::SerializableResource.new(@resource).as_json
      )
    }
  end

NOTE: Depending on your version of AMS, ActiveModel::SerializableResource might need to be replaced with ActiveModelSerializers::SerializableResource, but because of rails-api/active_model_serializers#1667 / #600, you're probably pinned to 0.10.0.rc4 like me, so the above is the version you want.

@zachfeldman
Copy link
Contributor

Workaround posted, closing for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants