Reputation: 2662
I use rails as backend for mobile app. I tried including versions for the Api. I use devise for authentication and now the authenticate_user! method is not working and it shows me the error
NoMethodError (undefined method `authenticate_user!' for
#<Api::V1::CategoryController:0x00000103b95088>)
I do not know where i am doing wrong. please help me resolve this problem. Any help is appreciated.
Update:
module Api
module V1
class CategoryController < ApplicationController
before_filter :authenticate_user!
Upvotes: 2
Views: 2502
Reputation: 1016
I had the same problem after versioning my API. It seems that Devise, after adding namespaces, Devise changes the authenticate_user! method to authenticate_api_v1_user! and also current_user to current_api_v1_user. That happens if you are namespacing like this:
namespace :api do
namespace :v1 do
devise_for :users do
resources :sessions, defaults: {format: :json}
end
end
end
In order to solve the issue, you have to define devise_for first, before the namespacing. That way you will be able to access authenticate_user and current_user.
devise_for :users, :controllers => { :sessions => "api/v1/sessions" }
devise_scope :user do
namespace :api do
namespace :v1 do
resources :sessions, defaults: {format: :json}
end
end
end
resources :users
I hope it helps!
Upvotes: 3
Reputation: 3732
I think, authenticate_user!
added to ApplicationController
.
If your Api controller inherit from another controller (not ApplicationController
), u need to include this helper to your Api controller manually.
Upvotes: 0