Knock: A [GET] on /users respond with 'NameError (uninitialized constant Knock)'

Created on 15 Mar 2016  路  7Comments  路  Source: nsarno/knock

I use rails 5.0.0.beta3 and rbenv and used this guide
With rbenv i created a new rails api project rbenv exec rails _5.0.0.beta3_ new levented --api
After that i scaffold my user bin/rails g scaffold user name:string family_name:string nick_name:string email:string

routes.rb

Rails.application.routes.draw do
  mount Knock::Engine => '/knock'
  resources :users
end

application_controller.rb

class ApplicationController < ActionController::API
  include Knock::Authenticable
end

users_controller.rb

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :update, :destroy]

  # GET /users
  def index
    @users = User.all

    render json: @users
  end
end

A [GET] to /users prints the following error

ActionController::RoutingError (uninitialized constant ApplicationController::Knock):

app/controllers/application_controller.rb:2:in `<class:ApplicationController>'
app/controllers/application_controller.rb:1:in `<top (required)>'
app/controllers/users_controller.rb:1:in `<top (required)>'
Started GET "/users" for 127.0.0.1 at 2016-03-15 20:57:53 +0100

What did i wrong?

Most helpful comment

I second @hackerghost93 current using rails 6 and getting the error uninitialized constant Knock::Authenticable on any route inheriting from ApplicationController

currently added

  • gem 'knock' to gemfile + ran bundle install
  • ran the generator rails generate knock:install
  • ran rails generate knock:token_controller app (app being model I am authenticating)
  • In my ApplicationController I currently have
class ApplicationController < ActionController::Base
  include Knock::Authenticable

  def authenticate_app
    app = authenticate_for(App)
    if(app.nil?)
      render json: {"errors": ["resource not found"]}, status: 404
    else
      app
    end
  end
end

currently, any request made to any controller inheriting from ApplicationController (through postman or rspec) results in the error

Failure/Error: include Knock::Authenticable

NameError:
  uninitialized constant Knock::Authenticable

EDIT
Doing some digging, I found the solution to this for rails 6 thanks to this comment thread. In your application.rb add this line

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module YOURRAILSAPPNAME
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0 and config.autoloader = :classic   <=== this line

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

All 7 comments

I haven't tested knock with rails 5 yet so I'm not sure. I'll let you know when I do.

I tested Knock (master) with Rails 5 beta4. It seems to work fine without any modifications :)

Closing this for now, but feel free to reopen if you still have an issue with the latest beta. If you do, please provide a failing test as well.

Thank you 馃憤

I have the same problem with knock 2.0 when I want to login.

routes.rb

Rails.application.routes.draw do
  namespace :api, defaults: {format: :json} do
    mount Knock::Engine => '/login'
    resources :users, :only => [:create]
  end
end

application_controller.rb

class ApplicationController < ActionController::API
  include ActionController::Serialization
  include Knock::Authenticable
end

error

Started POST "/api/login/auth_token" for ::1 at 2016-10-26 12:01:30 +0200
Processing by Knock::AuthTokenController#create as JSON
  Parameters: {"auth"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}, "auth_token"=>{"auth"=>{"email"=>"[email protected]", "password"=>"[FILTERED]"}}}
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms)
NameError (uninitialized constant Knock::Auth)

@YMonnier you have probably fixed this but for anyone else having this issue, you need to change the endpoint that you're posting to from auth_token to whatever entity you're authing against - probably user_token. I think this is a change brought in by Knock 2.0.

I have the same issue in rails 'rails', '~> 6.0.2', '>= 6.0.2.1' following the Readme steps does prompt the same issue referring to the line in application controller include Knock::Authenticable. Any help ?

I second @hackerghost93 current using rails 6 and getting the error uninitialized constant Knock::Authenticable on any route inheriting from ApplicationController

currently added

  • gem 'knock' to gemfile + ran bundle install
  • ran the generator rails generate knock:install
  • ran rails generate knock:token_controller app (app being model I am authenticating)
  • In my ApplicationController I currently have
class ApplicationController < ActionController::Base
  include Knock::Authenticable

  def authenticate_app
    app = authenticate_for(App)
    if(app.nil?)
      render json: {"errors": ["resource not found"]}, status: 404
    else
      app
    end
  end
end

currently, any request made to any controller inheriting from ApplicationController (through postman or rspec) results in the error

Failure/Error: include Knock::Authenticable

NameError:
  uninitialized constant Knock::Authenticable

EDIT
Doing some digging, I found the solution to this for rails 6 thanks to this comment thread. In your application.rb add this line

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module YOURRAILSAPPNAME
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0 and config.autoloader = :classic   <=== this line

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

I second @hackerghost93 current using rails 6 and getting the error uninitialized constant Knock::Authenticable on any route inheriting from ApplicationController

currently added

  • gem 'knock' to gemfile + ran bundle install
  • ran the generator rails generate knock:install
  • ran rails generate knock:token_controller app (app being model I am authenticating)
  • In my ApplicationController I currently have
class ApplicationController < ActionController::Base
  include Knock::Authenticable

  def authenticate_app
    app = authenticate_for(App)
    if(app.nil?)
      render json: {"errors": ["resource not found"]}, status: 404
    else
      app
    end
  end
end

currently, any request made to any controller inheriting from ApplicationController (through postman or rspec) results in the error

Failure/Error: include Knock::Authenticable

NameError:
  uninitialized constant Knock::Authenticable

EDIT
Doing some digging, I found the solution to this for rails 6 thanks to this comment thread. In your application.rb add this line

require_relative 'boot'

require 'rails/all'

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module YOURRAILSAPPNAME
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0 and config.autoloader = :classic   <=== this line

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

thank you, helped me a lot!

Was this page helpful?
0 / 5 - 0 ratings