Why rails can't find my sweepers? - ruby-on-rails

I have on my controller:
class FormulariosController < ApplicationController
cache_sweeper :campanha_sweeper, :only => [:show]
# actions
end
I've tried to create a directory called /sweepers inside app and it didn't work, I've tried inside models to create a /cache folder(there were a blog using this as example) and it didn't work.
My sweeper looks like this:
class CampanhaSweeper < ActionController::Caching::Sweeper
observe Campanha
def after_update(campanha)
expire_cache_for(campanha)
end
private
def expire_cache_for(campanha)
expire_page(:controller => 'formularios', :action => 'show')
ap "Expired cache for formularios#show in formulariosweeper"
end
end
I get when trying to start the server the following:
/Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.12/lib/action_controller/caching/sweeping.rb:41:in `const_get': uninitialized constant CampanhaSweeper (NameError)
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.12/lib/action_controller/caching/sweeping.rb:41:in `block in cache_sweeper'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.12/lib/action_controller/caching/sweeping.rb:39:in `each'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.12/lib/action_controller/caching/sweeping.rb:39:in `cache_sweeper'
from /Users/thiagomassa/Projects-ginga/ginga-campanhas/app/controllers/formularios_controller.rb:2:in `<class:FormulariosController>'
from /Users/thiagomassa/Projects-ginga/ginga-campanhas/app/controllers/formularios_controller.rb:1:in `<top (required)>'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/engine.rb:138:in `block (2 levels) in eager_load!'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/engine.rb:137:in `each'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/engine.rb:137:in `block in eager_load!'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/engine.rb:135:in `each'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/engine.rb:135:in `eager_load!'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/application.rb:108:in `eager_load!'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/application/finisher.rb:41:in `block in <module:Finisher>'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/initializable.rb:25:in `instance_exec'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/initializable.rb:25:in `run'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/initializable.rb:50:in `block in run_initializers'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/initializable.rb:49:in `each'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/initializable.rb:49:in `run_initializers'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/application.rb:134:in `initialize!'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/application.rb:77:in `method_missing'
from /Users/thiagomassa/Projects-ginga/ginga-campanhas/config/environment.rb:5:in `<top (required)>'
from /Users/thiagomassa/Projects-ginga/ginga-campanhas/config.ru:3:in `block in <main>'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/rack-1.2.5/lib/rack/builder.rb:46:in `instance_eval'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/rack-1.2.5/lib/rack/builder.rb:46:in `initialize'
from /Users/thiagomassa/Projects-ginga/ginga-campanhas/config.ru:1:in `new'
from /Users/thiagomassa/Projects-ginga/ginga-campanhas/config.ru:1:in `<main>'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/rack-1.2.5/lib/rack/builder.rb:35:in `eval'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/rack-1.2.5/lib/rack/builder.rb:35:in `parse_file'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/rack-1.2.5/lib/rack/server.rb:162:in `app'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/rack-1.2.5/lib/rack/server.rb:253:in `wrapped_app'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/rack-1.2.5/lib/rack/server.rb:204:in `start'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/commands/server.rb:65:in `start'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/commands.rb:30:in `block in <top (required)>'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/commands.rb:27:in `tap'
from /Users/thiagomassa/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.0.12/lib/rails/commands.rb:27:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Why didn't it work? Also, is there any way to expire cache of only a view(of how it's rendered). I'm generating views on the fly and it would be very useful if I could expire the cache of a view that is located at say, views/formularios/a.html.rb
If guys want to know what I'm doing, it's simple. I generate views with forms and when I want to update that view(or it's code) and I'm in production, the view doesn't get updated due to caching so I have to manually reset it's cache everytime I "update" a form.
Thanks.

Did you add the sweepers directory (or where ever you've put the sweeper code) to your autoload_paths in application.rb?

Related

Run code on the start of a rails server

When the Ruby on Rails application starts I want it to run a block of code so that the database is filled with the required data. I've been searching for hours for a solution, none of the given solutions that I found worked. The following is the solution that I implemented.
In application.rb I placed the following block of code:
def fill_db
redirect_to url_for(:controller => :item, :action => :init)
self.delay(:run_at => 10.minute.from_now).fill_db
end
config.after_initialize do
redirect_to url_for(:controller => :item, :action => :init)
fill_db
end
I tried to implement dozens of other variants of this but none of them worked.
The error I get is this:
C:\xampp\htdocs\dev-ruby\untitled1>rails s
=> Booting WEBrick
=> Rails 4.2.6 application starting in development on http://localhost:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
Exiting
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/railtie.rb:196:in `method_missing': undefined method `url_for' for Untitled1::Application:Class (NoMethodError)
from C:/xampp/htdocs/dev-ruby/untitled1/config/application.rb:40:in `block in <class:Application>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:36:in `call'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:45:in `block in run_load_hooks'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:44:in `each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/activesupport-4.2.6/lib/active_support/lazy_load_hooks.rb:44:in `run_load_hooks'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/application/finisher.rb:62:in `block in <module:Finisher>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `instance_exec'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/initializable.rb:30:in `run'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/initializable.rb:55:in `block in run_initializers'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `call'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/initializable.rb:54:in `run_initializers'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/application.rb:352:in `initialize!'
from C:/xampp/htdocs/dev-ruby/untitled1/config/environment.rb:5:in `<top (required)>'
from C:/xampp/htdocs/dev-ruby/untitled1/config.ru:3:in `require'
from C:/xampp/htdocs/dev-ruby/untitled1/config.ru:3:in `block in <main>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `instance_eval'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:55:in `initialize'
from C:/xampp/htdocs/dev-ruby/untitled1/config.ru:in `new'
from C:/xampp/htdocs/dev-ruby/untitled1/config.ru:in `<main>'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `eval'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:49:in `new_from_string'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/builder.rb:40:in `parse_file'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:299:in `build_app_and_options_from_config'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:208:in `app'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/commands/server.rb:61:in `app'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/rack-1.6.4/lib/rack/server.rb:336:in `wrapped_app'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/commands/server.rb:139:in `log_to_stdout'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/commands/server.rb:78:in `start'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:80:in `block in server'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `tap'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `server'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/railties-4.2.6/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
The same code works in a controller, but then it's not executed at the start of the rails server.
You cannot use redirect_to inside application.rb. This is part of the ActionController classes. Here are two things that you can do instead
1) Implement db:seed - there is a db/seed.rb that you can use to populate initial database values. After you deploy the code just run bin/rails db:seed to run this task that will fill out the database
or
2) Add before_action to applicationController
class ApplicationController < ActionController::Base
before_action :fill_db
end
To prevent from executing this method add conditional like unless populated?
Hope it helps

Uninitialized constant error on spree upgrade

I am trying to upgrade from spree 0.7 to spree 1.0.7. After successful bundle install I am fetting the following error:
/config/initializers/product_review_ratings.rb:12:in `<class:Config>': uninitialized constant Spree::ProductReviewsRatings::Config::PreferenceAccess (NameError)
from /home/nish/repos/spree/voylla_website_production/config/initializers/product_review_ratings.rb:10:in `<module:ProductReviewsRatings>'
from /home/nish/repos/spree/voylla_website_production/config/initializers/product_review_ratings.rb:2:in `<module:Spree>'
from /home/nish/repos/spree/voylla_website_production/config/initializers/product_review_ratings.rb:1:in `<top (required)>'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/engine.rb:556:in `block (2 levels) in <class:Engine>'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/engine.rb:555:in `each'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/engine.rb:555:in `block in <class:Engine>'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/initializable.rb:30:in `instance_exec'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/initializable.rb:30:in `run'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/initializable.rb:55:in `block in run_initializers'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/initializable.rb:54:in `each'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/initializable.rb:54:in `run_initializers'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/application.rb:96:in `initialize!'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /home/nish/repos/spree/voylla_website_production/config/environment.rb:7:in `<top (required)>'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/polyglot-0.3.4/lib/polyglot.rb:65:in `require'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/polyglot-0.3.4/lib/polyglot.rb:65:in `require'
from /home/nish/repos/spree/voylla_website_production/config.ru:3:in `block in <main>'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/rack-1.3.10/lib/rack/builder.rb:51:in `instance_eval'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/rack-1.3.10/lib/rack/builder.rb:51:in `initialize'
from /home/nish/repos/spree/voylla_website_production/config.ru:in `new'
from /home/nish/repos/spree/voylla_website_production/config.ru:in `<main>'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/rack-1.3.10/lib/rack/builder.rb:40:in `eval'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/rack-1.3.10/lib/rack/builder.rb:40:in `parse_file'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/rack-1.3.10/lib/rack/server.rb:200:in `app'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/commands/server.rb:46:in `app'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/rack-1.3.10/lib/rack/server.rb:304:in `wrapped_app'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/rack-1.3.10/lib/rack/server.rb:254:in `start'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/commands/server.rb:70:in `start'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/commands.rb:54:in `block in <top (required)>'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/commands.rb:49:in `tap'
from /home/nish/.rvm/gems/ruby-1.9.2-p320#spreeTest/gems/railties-3.1.12/lib/rails/commands.rb:49:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
/config/initializers/product_review_ratings.rb
module Spree
module ProductReviewsRatings
class Config
include Singleton
include PreferenceAccess
class << self
def instance
return nil unless ActiveRecord::Base.connection.tables.include?('configurations')
ProductReviewsRatingsConfiguration.find_or_create_by_name("Default product reviews and ratings configuration")
end
end
end
end
end
I tried to google, but could not find anything
PreferenceAccess was removed from Spree 1.0.0 by this commit:
https://github.com/spree/spree/commit/a4a57db941eeb6329ac9a5de64f2175e33a56f95
You can add preference_access.rb to your application in order to bring that functionality back, however, this will most likely lead to something else breaking.
If possible, you may want to consider porting from SpreeProductReviewsRatings to the spree_reviews extension, which currently supports Spree 1.0 - 2.3.0.beta.

uninitialized constant ApplicationController::CanCan (NameError)

My application runs normally on development environment but I am getting the following error when I try to run > rails server -e production
I have searched for this and I found answers about restarting unicorn but I don't have unicorn in my application, what can I do to fix it ?
/app/controllers/application_controller.rb:6:in `<class:ApplicationController>': uninitialized constant ApplicationController::CanCan (NameError)
from /home/dexter/Desktop item/Triton/app/controllers/application_controller.rb:1:in `<top (required)>'
from /var/lib/gems/1.9.1/gems/ckeditor-4.0.4/app/controllers/ckeditor/application_controller.rb:1:in `<top (required)>'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/engine.rb:439:in `block (2 levels) in eager_load!'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/engine.rb:438:in `each'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/engine.rb:438:in `block in eager_load!'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/engine.rb:436:in `each'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/engine.rb:436:in `eager_load!'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/application/railties.rb:8:in `each'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/application/railties.rb:8:in `all'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/engine.rb:434:in `eager_load!'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/application/finisher.rb:53:in `block in <module:Finisher>'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `instance_exec'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:30:in `run'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:55:in `block in run_initializers'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `each'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/initializable.rb:54:in `run_initializers'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/application.rb:136:in `initialize!'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /home/dexter/Desktop item/Triton/config/environment.rb:5:in `<top (required)>'
from /home/dexter/Desktop item/Triton/config.ru:3:in `block in <main>'
from /var/lib/gems/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `instance_eval'
from /var/lib/gems/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `initialize'
from /home/dexter/Desktop item/Triton/config.ru:in `new'
from /home/dexter/Desktop item/Triton/config.ru:in `<main>'
from /var/lib/gems/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:40:in `eval'
from /var/lib/gems/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:40:in `parse_file'
from /var/lib/gems/1.9.1/gems/rack-1.4.5/lib/rack/server.rb:200:in `app'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands/server.rb:46:in `app'
from /var/lib/gems/1.9.1/gems/rack-1.4.5/lib/rack/server.rb:304:in `wrapped_app'
from /var/lib/gems/1.9.1/gems/rack-1.4.5/lib/rack/server.rb:254:in `start'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands/server.rb:70:in `start'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:55:in `block in <top (required)>'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:50:in `tap'
from /var/lib/gems/1.9.1/gems/railties-3.2.12/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
My application controller:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :authenticate_user!
rescue_from CanCan::AccessDenied do |exception|
flash[:error] = exception.message
redirect_to root_path
end
private
def stored_location_for(resource_or_scope)
nil
end
def after_sign_in_path_for(resource_or_scope)
'/dashboard'
end
end
Gemfile > just posted the place of CanCan
group :assets do
gem 'cancan'
end
Remove cancan from your assets group in your Gemfile. The assets group are gems that are not to be used in production. Therefore when you run the application in production mode, gems in the assets block will be excluded.

Rails superclass mismatch error with two controllers

I have two controllers
one is defined as
class Admin::Businesses::DealsController < AdminController
it is located at app/controllers/admin/businesses/deals_controller.rb
the other
class Admin::DealsController < AdminController
is located at app/controllers/admin/deals_controller.rb
Everything works find in development env but When I push to heroku and the app loads in production env it gives me this error
/app/app/controllers/businesses/deals_controller.rb:1:in `<top (required)>': superclass mismatch for class DealsController (TypeError)
related lines from my routes.rb look like this
namespace 'admin' do
resources :deals do
end
resources :businesses do
resources :deals, module: "businesses"
end
end
Why is it giving me this error, due to same name of controllers ? or super class mismatch ? How can I fix this. I want two controllers for deals one under admin and the other under admin/businesses for business specific actions.
Here is the full stack trace when trying to run the app locally in prod env using
rails s -e production
/home/abid/Projects/vocallocal/app/controllers/admin/businesses/deals_controller.rb:1: warning: toplevel constant Businesses referenced by Admin::Businesses
Exiting
/home/abid/Projects/vocallocal/app/controllers/businesses/deals_controller.rb:1:in `<top (required)>': superclass mismatch for class DealsController (TypeError)
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:359:in `require_or_load'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:313:in `depend_on'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:225:in `require_dependency'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/engine.rb:439:in `block (2 levels) in eager_load!'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/engine.rb:438:in `each'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/engine.rb:438:in `block in eager_load!'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/engine.rb:436:in `each'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/engine.rb:436:in `eager_load!'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/application/finisher.rb:53:in `block in <module:Finisher>'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `instance_exec'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/initializable.rb:30:in `run'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/initializable.rb:55:in `block in run_initializers'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `each'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/initializable.rb:54:in `run_initializers'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/application.rb:136:in `initialize!'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /home/abid/Projects/vocallocal/config/environment.rb:5:in `<top (required)>'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `block in require'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:236:in `load_dependency'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:251:in `require'
from /home/abid/Projects/vocallocal/config.ru:4:in `block in <main>'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:51:in `instance_eval'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:51:in `initialize'
from /home/abid/Projects/vocallocal/config.ru:1:in `new'
from /home/abid/Projects/vocallocal/config.ru:1:in `<main>'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:40:in `eval'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/builder.rb:40:in `parse_file'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:200:in `app'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/server.rb:46:in `app'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:301:in `wrapped_app'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/rack-1.4.1/lib/rack/server.rb:252:in `start'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands/server.rb:70:in `start'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:55:in `block in <top (required)>'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:50:in `tap'
from /usr/local/ruby/lib/ruby/gems/1.9.1/gems/railties-3.2.3/lib/rails/commands.rb:50:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Thanks
The problem is that your admin namespace in your routes is conflicting with your admin module in your class hierarchy. The details about why this is causing that specific error are complicated, but are explained in more detail at http://code.dblock.org/warning-toplevel-constant-xyz-referenced-adminxyz
The simplest solution is to rename one of your admins, probably the class name, to anything else.

ActiveAdmin Comment model not working properly

I created a blog by following the Getting Started with Rails precisely. And then I following the tutorial here to try ActiveAdmin: http://activeadmin.info/documentation.html.
It's working fine for the Post and Tag models (I logged in and created/add/edited stuff) but not the Comment model.
rails generate active_admin:resource Comment
create app/admin/comments.rb
Then when I try to start the server I get this:
rails server=> Booting WEBrick
=> Rails 3.0.7 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server Exiting /usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin/namespace.rb:116:in
`find_or_build_resource': Tried to
register Comment as Comment but
already registered to Comment
(ActiveAdmin::ResourceMismatchError)
from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin/namespace.rb:45:in
`register' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:141:in
`register' from
/Users/myusername/Projects/RoR/blog/app/admin/comments.rb:1:in
`<top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`block in load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`block in load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in
`new_constants_in' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:in
`block in load!' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:in
`each' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:185:in
`load!' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:212:in
`routes' from
/Users/myusername/Projects/RoR/blog/config/routes.rb:2:in
`block in <top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/route_set.rb:233:in
`instance_exec' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/routing/route_set.rb:233:in
`draw' from
/Users/myusername/Projects/RoR/blog/config/routes.rb:1:in
`<top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`block in load' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`block in load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in
`new_constants_in' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:235:in
`load' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:in `block in reload_routes!' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:in `each' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:127:in `reload_routes!' from
/usr/local/lib/ruby/gems/1.9.1/gems/activeadmin-0.2.2/lib/active_admin.rb:123:in
`block in setup' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/callbacks.rb:420:in
`_run_prepare_callbacks' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/callbacks.rb:40:in
`initialize' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:33:in
`new' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:33:in
`build' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`block in build' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`each' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`inject' from
/usr/local/lib/ruby/gems/1.9.1/gems/actionpack-3.0.7/lib/action_dispatch/middleware/stack.rb:79:in
`build' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:162:in `app' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application/finisher.rb:35:in
`block in <module:Finisher>' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:25:in `instance_exec' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:25:in `run' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:50:in `block in run_initializers' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:49:in `each' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/initializable.rb:49:in `run_initializers' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:134:in `initialize!' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/application.rb:77:in
`method_missing' from
/Users/myusername/Projects/RoR/blog/config/environment.rb:5:in
`<top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in
`require' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in
`block in require' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`block in load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in
`new_constants_in' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:225:in
`load_dependency' from
/usr/local/lib/ruby/gems/1.9.1/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:239:in
`require' from
/Users/myusername/Projects/RoR/blog/config.ru:3:in
`block in <main>' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in
`instance_eval' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:46:in
`initialize' from
/Users/myusername/Projects/RoR/blog/config.ru:1:in
`new' from
/Users/myusername/Projects/RoR/blog/config.ru:1:in
`<main>' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in
`eval' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/builder.rb:35:in
`parse_file' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:162:in
`app' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:248:in
`wrapped_app' from
/usr/local/lib/ruby/gems/1.9.1/gems/rack-1.2.1/lib/rack/server.rb:213:in
`start' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands/server.rb:65:in `start' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:30:in
`block in <top (required)>' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:27:in
`tap' from
/usr/local/lib/ruby/gems/1.9.1/gems/railties-3.0.7/lib/rails/commands.rb:27:in
`<top (required)>' from
script/rails:6:in `require' from
script/rails:6:in `<main>'
Your help would be appreciated.
Look like a bug (ActiveAdmin has it's own built-in Comment model/class already): https://github.com/gregbell/active_admin/issues/64
A possible workaround could be to give your Comment model a different name within in app/admin/comments.rb:
ActiveAdmin.register Comment, :as => "PostComment" do
try this
run in terminal
rails g active_admin:resource ActiveAdmin::Comment
add a following lines in
app/admin/active_admin_comment.rb
ActiveAdmin.register ActiveAdmin::Comment, :as => "Comment" do
end
after that edit config/initializers/active_admin
[...]
config.comments_registration_name = "OldComment"
config.show_comments_in_menu = false
[...]
And now you have opportunity to customize a comments in app/admin/active_admin_comment.rb
but i still didn't find solution to add :update and :destroy actions
I had the same problem with version 1.0.0.pre, but thankfully, you can suppress ActiveAdmin comments altogether with a config in active_admin.rb then you don't have to alias your own Comment model anymore as mentioned above.
config/initializers/active_admin.rb
...
# == Admin Comments
#
# This allows your users to comment on any resource registered with Active Admin.
#
# You can completely disable comments:
config.allow_comments = false
#
It's also listed in the docs: https://github.com/gregbell/active_admin/blob/master/docs/1-general-configuration.md#comments
Dylan's answer is a great start. In addition to his, I needed to overwrite the controller resource/collection methods.
app/admin/comments.rb
ActiveAdmin.register Comment, as: 'QuestionComment' do
controller do
defaults resource_class: Comment, collection_name: 'comments', instance_name: 'comment'
end
end
Basically, you're just overwriting the defaults of inherited resource controller (https://github.com/josevalim/inherited_resources#overwriting-defaults)
(yes, this is still an issue in 2014 even in the 1.0.0 prerelease version, but it appears that they want to fix it from reading the github issues)

Resources