active_model_serializers icon indicating copy to clipboard operation
active_model_serializers copied to clipboard

Is the existence of 'current_user' mandatory?

Open raulbrito opened this issue 14 years ago • 5 comments

I'm creating a Rails app from scratch and still don't have any authentication mechanism in place. I had to specify a current_user method in the controller in order to produce json from the active_model_serializer, otherwise i get an exception: NoMethodError in EntriesController#index

undefined method `current_user' for #EntriesController:0x007fc14cb4a8d8

raulbrito avatar Feb 23 '12 19:02 raulbrito

Yes, it is mandatory. IF you don't have a current_user, you may as well return nil for now.

josevalim avatar May 02 '12 10:05 josevalim

Thanks for the update, how would you deal with multiple user resources. Example, having current_user and current_admin depending on the controller.

Is this possible as of today? If no, is it meant to move toward this path?

ZenCocoon avatar May 02 '12 10:05 ZenCocoon

current_user would have to change based on what is signed in, the admin or the current user. If I am not wrong, there is a way to tell active model serializers to use another method, like current_serialization_scope, take a look at the source.

josevalim avatar May 02 '12 10:05 josevalim

thanks for the tip, I'll dig the source for more then. Thanks a lot for your awesome work.

ZenCocoon avatar May 02 '12 10:05 ZenCocoon

current_user is not mandatory anymore as of https://github.com/josevalim/active_model_serializers/commit/d5d917ea5687e313069c5b53050c470e236f3184

See https://github.com/josevalim/active_model_serializers/blob/d5d917ea5687e313069c5b53050c470e236f3184/lib/action_controller/serialization.rb#L12 for more details and how to use serialization_scope, it can be current_user or anything you like.

ZenCocoon avatar May 04 '12 13:05 ZenCocoon