I'm getting the following error message when trying to access the admin/admin_users route provided by the Administrate gem: LoadError in Admin::AdminUsersController#index. The other admin routes (i.e. Users, Topics, Posts) work fine. I haven't changed any of the other default configuration for Administrate.
I encountered the bug while working on a code walkthrough available here: https://rails.devcamp.com/professional-rails-development-course/advanced-user-features/customizing-forms-administrate-dashboard. The URL contains a link to the repo; my local version is identical aside from using rails 5.
Portions of relevant files are included below. Any idea what might be causing this error?
localhost:3000/admin/admin_users
Unable to autoload constant Admin::AdminUsersController, expected .../app/controllers/admin/admin_users_controller.rb to define it
...
else
require_or_load(expanded, qualified_name)
raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined? (const_name, false)
return from_mod.const_get(const_name)
end
elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
../app/controllers/admin_user_controller.rb
module Admin
class ApplicationController < Administrate::ApplicationController
end
end
../config/routes.rb
Rails.application.routes.draw do
namespace :admin do
resources :users
resources :topics
resources :admin_users
resources :posts
root to: "users#index"
end
...
../vendor/gemfile.rb
...
gem "administrate", "~> 0.3.0"
gem 'bourbon'
Related
I'm using Rails 5 and I'm getting this error when calling a method in my Api. The thing is, the error only happens sometimes.
LoadError (Unable to autoload constant Api::V1::UsersController, expected /home/user/projects/project-name/app/controllers/api/v1/users_controller.rb to define it):
Relevant part of routes.rb:
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
resources :users
end
end
end
Relevant part of the controller:
class Api::V1::UsersController < ApplicationController
I'm clueless of whats wrong, google a lot but couldn't find a solution.
Have you tried adding to the top of your controller
module Api
module V1
class className < ApplicationController
....
end
end
end
This should follow your directory structure.
I am using Knock for JWT authentication for Rails 5 API.
I have this Routes file:
Rails.application.routes.draw do
namespace :api, constraints: { subdomain: 'api' }, path: '/' do
namespace :v1 do
post 'user_token' => 'user_token#create'
end
end
end
With this, I expect to be able to make a POST request to create new token like this:
POST http://api.domain.com/v1/user_token
but this request gives me the following error:
NameError: uninitialized constant API::V1::User
What I can understand is that Knock is trying to access the model User on the same namespace where the controller (user_token_controller) is. But my model are not namespaced:
class User < ApplicationRecord
has_secure_password
#...
end
My user_token_controller.rb
module API
module V1
class UserTokenController < Knock::AuthTokenController
end
end
end
What I dong wrong?
It's a bug already fixed but not yet published.
The temporary solution was using the gem from github code like:
gem 'knock', github: 'psantos10/knock', branch: 'master'
https://github.com/nsarno/knock/issues/120
I'm having a problem creating an Engine in Rails.
I want to create an engine that uses Devise to share over all my other applications.
I followed the Getting Started guide for Engines at rails website:
http://edgeguides.rubyonrails.org/engines.html
Created an application, required my engine... everything runs just fine.
Then, I run into Devise (I've already installed Devise direct in an application before, but not in an engine) but when I try to use it, by acessing any controller that inherits the before_action authenticate_user! I get this message:
http://i.imgur.com/dIIxgwU.jpg
I really don't understand (I'm starting in Ruby, I'm a CakePHP guy), the route is set, but it can't find it (?).
engine/config/initializers/devise.rb
Devise.setup do |config|
...
config.router_name = :doisbit
config.parent_controller = 'Doisbit::ApplicationController'
end
engine/config/routes.rb
Doisbit::Engine.routes.draw do
...
devise_for :users, class_name: "Doisbit::User", module: :devise
...
end
engine/lib/doisbit/engine.rb
module Doisbit
class Engine < ::Rails::Engine
isolate_namespace Doisbit
end
end
engine/lib/doisbit.rb
require 'doisbit/engine'
require 'devise'
...
engine/doisbit.gemspec
...
Gem::Specification.new do |s|
...
s.add_dependency "devise"
end
engine/app/controllers/application_controller.rb
module Doisbit
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
end
application/config/routes.rb
Rails.application.routes.draw do
...
mount Doisbit::Engine, at: "/doisbit"
end
application/config/environments/development.rb
Rails.application.configure do
...
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
end
application/Gemfile
...
gem 'doisbit', path: "C:/.../doisbit"
...
EDIT
Just noticed by accessing /doisbit/users/sign_in I get the form. (duh)
i.imgur.com/anTRK2d.jpg
But... still... by default it routes to /users/sign_in when i'm not logged in.
What I'm missing?
This might be a really stupid question. But I am starting to create a rails API and created a skeleton api with one controller index action which returns a static json.
Here is the sequence of steps I followed:
rails-api new my-app
My gemfile has:
gem 'rails', '4.1.8'
My routes are as follows:
MyApp::Application.routes.draw do
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :projects
end
end
end
here is my controller:
module Api
module V1
class ProjectsController < ApplicationController
def index
render json: {message: 'Resource not found'}, status: 404
end
end
end
end
Here is my model which has nothing:
class Project < ActiveRecord::Base
end
So when I try run the app rails s and go to:
localhost:3000/certificates/ or localhost:3000/api/v1/certificates
I get the following error:
ActiveRecord::ConnectionNotEstablished
Eventually I am going to add a database and retrieve results from a real table, but I was wondering shouldn't my above code return the json "Resource not found" instead of trying to connect to active record.
Does it always require an activerecord connection? I am trying to understand how rails-api works.
When you run the server ActiveRecord gets loaded automatically, and it will whine since it wants a db to connect to. Ìf you really need to, you can "deactivate" ActiveRecord on startup by following this answer.
Following this tutorial, I created the API of my existing blog web application on Rails. I am getting the error:
uninitialized constant API
This is my code:
lib/api/v1/articles.rb:
module API
module V1
class Articles < Grape::API
version 'v1'
format :json
resource :articles do
desc "Return list of recent posts"
get do
Article.recent.all
end
end
end
end
end
lib/api/v1/root/rb
module API
module V1
class Root < Grape::API
mount API::V1::Articles
end
end
end
lib/api/root.rb
module API
class Root < Grape::API
prefix 'api'
mount API::V1::Root
end
end
lib/tasks/routes.rake
namespace :api do
desc "API Routes"
task :routes => :environment do
API::Root.routes.each do |api|
method = api.route_method.ljust(10)
path = api.route_path.gsub(":version", api.route_version)
puts " #{method} #{path}"
end
end
end
config/routes.rb
Rails.application.routes.draw do
mount API::Root => '/'
get 'welcome/index'
root 'welcome#index'
resources :articles
end
This is the existing web application code:
app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def new
end
def create
# render plain: params[:article].inspect
#article = Article.new(article_params)
#article.save
redirect_to #article
end
def show
#article = Article.find(params[:id])
end
private
def article_params
params.require(:article).permit(:title,:text)
end
end
Now I'm in the Accessing API routes part of this article. When I run rake routes it gives the error, uninitialized constant API.. What I'm doing wrong.
Edit: As per the comment, giving the detailed error
rake aborted!
NameError: Uninitialized constant API
F:/blog/config/routes.rb:2:in 'block in <top (required)>'
F:/blog/config/routes.rb:1:in '<top (required)>'
C:in 'execute_if_updated'
F:/blog/config/environment.rb:5:in '<top (required)>'
Tasks: TOP =>routes =>environment
Contents of environment.rb
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Rails.application.initialize!
When I insert config.autoload_paths += Dir["#{config.root}/lib/**/"] in your config/application.rb, and run rake routes, it gives the error can't convert Symbol into String
Upgrading the grape version, I got this error:
As per the discussion, you need to update your grape gem's version to 0.9.0 and then you need to add this line in your Gemfile:
gem 'grape', '0.9.0'
and then:
$ bundle install
As of version 0.11.0, the documentation says
Rails
Place API files into app/api. Rails expects a subdirectory that
matches the name of the Ruby module and a file name that matches the
name of the class. In our example, the file name location and
directory for Twitter::API should be app/api/twitter/api.rb.
Simple solution would be creating another api folder and moving entire files & dirs into app/api/api folder