Ruby on Rails : uninitialized constant AdminController - ruby

Routing Error ! uninitialized constant AdminController
This appears to me when trying to go to
localhost:3000/admin
I have been searching a lot , but still stuck!
Rails 4.2.4
Ruby 2.2.3
Activeadmin 1.0.0 pre2
My Gem file :5dPDVf http://ideone.com/5dPDVf
My routes.rb File : dF6EFThttp://ideone.com/dF6EFT

Did you run rails generate active_admin:install ?

Looks like you need to create an AdminsController class like this:
in app/controllers/admins_controller.rb
class AdminsController < ApplicationController
def index
end
end
You might want to read Action Controller Overview
You might have it defined as AdminsController (plural) so you might need to adjust your routes to this instead of creating an AdminsController class.
The controller name should be plural (this is a naming convention in Rails).
routes.rb
resources :admins
You'll also need a view file inapp/views/admins/ called index.html.erb
and then visit http://localhost:3000/admins.

Related

Routing in rails 5.2

I have a RoR app with an admin_controller, the admin is able to CRUD Item, now I am a bit confused when it comes to routing. How can I create the app in a way that I can have a link to new_item be like: admin/item/new instead of creating an item controller with a authenticate_admin? method (since I use Device)
All in all how is this kind of routing achieved in rails
e.g. admin/orders/:id
or
store/sales/lates
Ideally , your admin_controller shouldn't do any CURD actions on an Item object. You should use an items_controller for that.
to achieve what you are trying to do, you can use an admin name space and maybe get the item_controller to be inherited from the admin controller.
So, you can have something like,
#app/controllers
AdminController < ApplicationController
end
#app/controllers/admin/
module Admin
ItemsController < AdminController
end
end
#config/routes
namespace :admin do
resources :items
end
rails namespaces

Rails 3 - Different Class Behavior in rails console vs rails server

I'm referencing a custom class from within a model named Plac
Model is defined in models/plac.rb like this:
class Model < ActiveRecord::Base
def notify_owner
notifier = BatchNotify.getInstance
end
end
BatchNotify is defined in lib/modules/batch_notify.rb like so:
class BatchNotify
def self.getInstance
env = Rails.env
if(env == "test")
return TestBatchNotify.new
else
BatchNotify.new
end
end
end
I have also added the modules directory to autoload_path:
config.autoload_paths += %W(#{config.root}/lib/modules)
The weird thing is that when notify_owner() works great from the rails console.
However, when I start the web server with rails server and try to trigger notify_owner by using the app in the browser, I get the following error:
uninitialized constant Plac::BatchNotify
First, why is the behavior different in console vs web server?
Second, why does it still not recognize the Batch notify constant?
By the way, I've also tried defining BatchNotify within a module and referencing it as Module::BatchNotify with no luck...
There are a couple of ways to get the BatchNotify class loaded properly.
Add a config/initializers/00_requires.rb file with the following code:
require "#{Rails.root}/lib/modules/batch_notify.rb"
Or, require models/placebo.rb in the Model class:
require "#{Rails.root}/lib/modules/batch_notify.rb"
A couple of other comments on this code:
Rails already uses the term models, so Model is not a good class name in Rails.
The file naming convention is that the file name should correspond with the model name. So, the models/placebo.rb should be renamed to models/model.rb to follow convention.
BatchNotify is a class so I don't think you should put it a directory name modules.

NoMethodError Sinatra Modular app

Probably something quite basic but I want to be able to use some custom helper methods in a modular Sinatra app. I have the following in ./helpers/aws_helper.rb
helpers do
def aws_asset( path )
File.join settings.asset_host, path
end
end
and then in my view i want to be able to use this method like so
<%= image_tag(aws_asset('/assets/images/wd.png')) %>
but i get the above area, so within my app.rb file i am
require './helpers/aws_helper'
class Profile < Sinatra::Base
get '/' do
erb :index
end
end
So is my issue that i am requiring it outside of my Profile class. which doesn't make sense as I am requiring my config files for ENV variables the same way and they are being read, but then again they are not methods so i guess that does make sense.
I think maybe im struggling to get my head around what a modular app is as opposed to using a classic styled sinatra app.
Any pointers appreciated
Error message
NoMethodError at / undefined method `aws_asset' for #<Profile:0x007f1e6c4905c0> file: index.erb location: block in singletonclass line: 8
When you use helpers do ... in the top level like this you are adding the methods as helpers to Sinatra::Application and not your Profile class. If you are using the Sinatra modular style exclusively make sure you only ever use require 'sinatra/base', and not require sinatra, this will prevent you from mixing up the two styles like this.
In this case you should probably create a module for your helpers instead of using helpers do ..., and then add that module with the helpers method in your Profile class.
In helpers/aws_helper.rb:
module MyHelpers # choose a better name of course
def aws_asset( path )
File.join settings.asset_host, path
end
end
In app.rb:
class Profile < Sinatra::Base
helpers MyHelpers # include your helpers here
get '/' do
erb :index
end
end

Kaminari and Mongoid 'undefined method page' when model defined in separate gem

I have a simple logging application consisting of ruby scripts and a "read-only" Rails application that reads from the MongoDB documents. To keep the models consistent they both use a common gem where the Mongoid documents have been defined.
The problem I'm having is that Mongoid documents that are defined in the gem are not getting the Kaminari decoration. In other words (simplified as much as possible).
# User is a Mongoid document defined in the Rails app (i.e. standard behavior)
require 'kaminari'
class UsersController < ApplicationController
def index
User.page(params[:pg]) # works great
end
end
but
# SharedLogging::LogEntry is a Mongoid document defined in a separate gem
require 'kaminari'
class LogEntriesController < ApplicationController
def index
SharedLogging::LogEntry.page(params[:pg]) # undefined method 'page' for ...
end
end
I think the solution is to do something in /config/initializers/kaminari_config.rb to force the pagination to be applied to the shared models but I haven't been able to stumble across the correct solution.
Alternately, I've also tried adding Kaminari as a dependency in the shared gem, but no luck there.
It seems to be resolved with the same solution described here: https://github.com/collectiveidea/delayed_job_mongoid/issues/10
In my kaminari_config.rb I've added the following lines:
SharedLogging::LogEntry.send(:include, Kaminari::MongoidExtensions::Document)
SharedLogging::LogEntry.send(:include, Kaminari::MongoidExtensions::Criteria)
The first line is required if I do SharedLogging::LogEntry.page(params[:pg]) the second if I apply a scope first (e.g. SharedLogging::LogEntry.by_date(params[:dt]).page(params[:pg]) ).
The biggest problem is that I need both lines for each model in my gem; and there are a lot of models.
Rather than manually extending, use Kaminari's hooks initializer. There are details in another answer I posted:
undefined method page for #<Array:0xc347540> kaminari "page" error. rails_admin

getting all the Active Record models in app/models directory

I'm planning to create a ruby gem which requires to get all the ActiveRecord models from the directory (typically)
RAILS_ROOT/app/models
how can I get the list of model names (physical) in ruby (ruby 1.9)
cheers
sameera
So, if the class name matches with the file name, you can use something like this:
# models/user.rb
class User < ActiveRecord::Base
end
Dir.glob("./models/*.rb").each {|model| require model}
user = User.new
ActiveRecord::Base.subclasses.collect(&:name)
Returns all the model name.

Resources