I'm using the gem 'clockwork' in a Rails 3.2 app. The app is running on Heroku. The clock job runs at 1am - but it's not executing the code.
Here is the clock.rb code:
Clockwork.every(1.day, 'DailyJob', :at => '01:00'){
StatsMailer.stats_email.deliver
Forecast.where(:run_date => Date.today).each do |forecast|
woschedules_run_jobplans_path(:id => forecast.woschedule_id)
end
}
The log shows:
Sep 04 00:00:05 ndeavor-staging app/clock.1: #<NoMethodError: undefined method `woschedules_run_jobplans_path' for Clockwork:Module>
If I rake routes, I get:
woschedules_run_jobplans GET /woschedules/run_jobplans(.:format) woschedules#run_jobplans
The error is in that the route is non-existant. Since the route DOES exist, this problem usually means that the Rails routes have not been loaded into the current scope.
You may need to include Rails' route helpers in order for it to function properly: Rails.application.routes.url_helpers
Clockwork.every(1.day, 'DailyJob', :at => '01:00'){
StatsMailer.stats_email.deliver
Forecast.where(:run_date => Date.today).each do |forecast|
# Prepend your route with the following:
Rails.application.routes.url_helpers.woschedules_run_jobplans_path(:id => forecast.woschedule_id)
end
}
Alternatively, to dry it up a bit you can simply include all of the route helpers at the beginning of the file using:
# Beginning of file
include Rails.application.routes.url_helpers
# ...
Related
I'm using resque for background processing, the route /resque works fine in development, but in production (using passenger) it doesn't, the log/production.log shows the error:
ActionController::RoutingError (No route matches [GET] "/resque/overview")
when I run the command
rails routes | grep resque
it shows:
/resque
#<Resque::Server app_file="/var/www/webroot/ROOT/vendor/bundle/ruby/2.7.0/gems/resque-2.0.0/lib/resque/server.rb">
my Gemfile:
gem 'resque'
my config/initializers/resque.rb
require 'resque/server' # this is needed for resque web UI
redis_url = {
development: 'localhost:6379',
production: Redis.new(url: 'redis://{user}:{password}/{host_url}:6379/0')
}
rails_env = Rails.env.to_sym || 'development'
Resque.redis = redis_url[rails_env.to_sym]
my routes
authenticated :user, -> user { user.admin? } do
mount Resque::Server.new, :at => "/resque"
end
I will answer my question,
The issue was the following line
authenticated :user, -> user { user.admin? } do
Which seems to not work with my installed devise version 4.7.3
Once I get rid of it, the /resque route started to work! but I needed a way to restrict viewing that route for admin users only, so here is what I did,
in the config/initializers/resque.rb I added this:
require 'resque/server' # this is needed for resque web UI
class SecureResqueServer < Resque::Server
before do
redirect '/' unless request.env['warden'].user&.admin?
end
end
Then in routes.rb my /resque route becomes:
mount SecureResqueServer.new, :at => "/resque"
The above class and route are from Resque, Devise and admin authentication
That solved my issue... not sure but it seems that this issue is related to this:
https://github.com/heartcombo/devise/issues/5019
I want to use this gem (sitemap_generator)
sitemap_generator
To create my sitemap xml file for my site.
So i create sitemap.rb inside config folder
Then i put this code inside
require 'rubygems'
require 'sitemap_generator'
SitemapGenerator::Sitemap.default_host = 'https://xxxx.com/'
SitemapGenerator::Sitemap.create do
# add '/home', :changefreq => 'daily', :priority => 0.9
# add '/contact_us', :changefreq => 'weekly'
add '/'
add '/signup'
add '/login'
Activity.find_each do |activity|
add activity_show_path(activity.id), :lastmod => activity.created_at
end
end
SitemapGenerator::Sitemap.ping_search_engines # Not needed if you use the rake tasks
But when i run
ruby config/sitemap.rb
I always got this
uninitialized constant Activity (NameError)
So how can i fixed this
(I guess the problem from the model)
Thanks!
I always run it through the rake task, try this:
rake sitemap:refresh:no_ping
It's possible the rake task does the magic to make the application code available when that's running.
Update: probably a duplicate of Rails sitemap_generator Uninitialized Constant? (sorry I should have looked first)
I have the following in my routes.rb file for Rails 3:
13 namespace :user do
14 root :to => "users#profile"
15 end
I get this error on heroku:
ActionController::RoutingError (uninitialized constant User::UsersController):
I already restarted the application.
I am doing this because I am using devise and this is what it says on the wiki:
https://github.com/plataformatec/devise/wiki/How-To:-Redirect-to-a-specific-page-on-successful-sign-in
The problem is that Rails is expecting there to be a controller within a module called Users because that's what namespace :user infers. Perhaps you meant to use scope instead of namespace?
scope :path => "user" do
root :to => "users#profile"
end
Note: in this situation if you've only got one route it would not be wise to use scope, but if you've got multiple ones with the /user prefix then it would be fine to. If you only had one, I would do this instead:
get '/user', :to => "users#profile"
Heroku environments run in production mode. When you run locally, you run in development mode, which accounts for at least one difference. Try this instead:
RAILS_ENV=production bundle exec rails s
and see if you notice the same error.
I'm new to Ruby on Rails (formerly and currently PHP expert) so forgive my ignorance but I'm trying to get Sinatra working as middleware to redirect some old urls since I tried the gem rack-rewrite and couldn't get that to work either.
I am using code samples from ASCIIcast so in my routes.rb I have the following:
root :to => HomeApp
(^ I'm redirecting the root only for testing)
In my lib folder I have home_app.rb
class HomeApp < Sinatra::Base
get "/" do
"Hello from Sinatra"
end
end
When I start the server (or if its already running) it produces the error:
routes.rb:10: uninitialized constant HomeApp
Which seems that it just isn't aware of the lib/home_app.rb file.
I have included Sinatra in my Gemfile and ran bundle install and confirms it is included.
I just want to reroute old urls from my old site to my new ruby app but can't get any of this middleware/rack stuff working. All documentation assumes you aren't a total newb or is for RoR pre-3.0.
You don't need to use Sinatra if you want to redirect some URLs. You can use the new redirect method. See the Rails Dispatch article.
match "/stories/:year/:month/:day/:name" => redirect("/%{name}")
constraints :user_agent => /iPhone/, :subdomain => /^(?!i\.)/ do
match "*path" => redirect {|params, req| "http://i.myapp.com/#{req.fullpath}" }
end
In your specific case, the problem is that the HomeApp class is not loaded. Either add the /lib folder to your load path changing application.rb
config.autoload_paths += %W( #{config.root}/lib )
or require the file.
I'm trying to get a sinatra app as a subpath in my rails 3 app.
Specifically, the resque queuing system has a sinatra based web interface that I would like to have accessible through /resque on my usual rails app.
You can see the project here: http://github.com/defunkt/resque
I found some people talking about adding a rackup file and doing this sort of thing:
run Rack::URLMap.new( \
"/" => ActionController::Dispatcher.new,
"/resque" => Resque::Server.new
)
But I don't really know where to put that or how to make it run. My deployment is with passenger, but it would me nice to also have it running when I run 'rails server' too. Any suggestions?
--edit--
I've made some progress by putting the following in config/routes.rb:
match '/resque(/:page)', :to => Rack::URLMap.new("/resque" => Resque::Server.new)
Which seems to work pretty well, however it loses the public folder, (which is defined within the gem I guess), and as a result, there is no styling information, nor images.
You can setup any rack endpoint as a route in rails 3. This guide by wycats goes over what you are looking for and many of the other things you can do in rails3:
http://yehudakatz.com/2009/12/26/the-rails-3-router-rack-it-up/
For example:
class HomeApp < Sinatra::Base
get "/" do
"Hello World!"
end
end
Basecamp::Application.routes do
match "/home", :to => HomeApp
end
Yehuda (/Scott S)'s solution doesn't work for me with Rails 3.0.4 and Sinatra 1.2.1... setting :anchor => false in the matcher is the key:
# in routes.rb
match "/blog" => MySinatraBlogApp, :anchor => false
# Sinatra app
class MySinatraBlogApp < Sinatra::Base
# this now will match /blog/archives
get "/archives" do
"my old posts"
end
end
(answer c/o Michael Raidel - http://inductor.induktiv.at/blog/2010/05/23/mount-rack-apps-in-rails-3/)