Cannot connect to the model by rails sitemap_generator gem? - ruby-on-rails

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)

Related

Sprockets::Rails::Helper::AssetNotPrecompiled in ResqueWeb

I've been trying to fix this for about two days but I'm getting no where. I'm trying to get the resque-web page to show but I keep running into the error
Sprockets::Rails::Helper::AssetNotPrecompiled in ResqueWeb::Overview#show
Message shown
ActionView::Template::Error (resque_web/plugins/resque_scheduler/application.css):
12: file_path = "#{p.name.underscore.downcase}/application.css"
13: if (Rails.application.assets && Rails.application.assets.find_asset(file_path)) ||
14: (Rails.application.assets_manifest && Rails.application.assets_manifest.assets[file_path])
15: stylesheet_link_tag "#{p.name.underscore.downcase}/application"
16: end
17: end.join("\n").html_safe
18: %>
what is being called from the stack
def raise_unless_precompiled_asset(path)
raise Helper::AssetNotPrecompiled.new(path) if #check_precompiled_asset && !precompiled?(path)
end
end
end
gem versions
resque (2.0.0)
resque-multi-job-forks (0.5.1)
resque-scheduler (4.4.0)
resque-web (0.0.12)
sprockets (4.0.2, 3.7.2)
sprockets-rails (3.2.2)
routes.rb
require "resque_web"
require 'resque-scheduler'
require 'resque/scheduler/server'
Rails.application.routes.draw do
mount ResqueWeb::Engine => "/resque_web"
root to: "pages#home"
.
.
.
end
Ideally it will be 'admin/resque_web' for production purposes.
I've tried this fix by "bitterloa" https://github.com/resque/resque-web/issues/106 because my file structure is set the same using webpacker
I've looked and the development.rb to check that debugging is false
I've recompiled my assets after destroying them using sprock-rails rails assets:clobber
Any ideas where I can look or if anyone else has run into this problem lend a hand please.
Run commands from terminal shows that resque is running and accepting schedule jobs from my scheduler.yml file I just can't get the css for it to work.
If you need any more info let me know.
Thanks
resque comes with a built-in server. You do not need resque-web and it seems it is not maintained (last commit was on 2018).
Let's try this:
gem 'resque', require: 'resque/server'
# routes.rb
mount Resque::Server.new, :at => "admin/resque"
Make sure you allow just admins to access to that page in production. For that you could read about Route Constraints to do something like:
constraints IsResqueAllowed do
mount Resque::Server.new, :at => "admin/resque"
end
class IsResqueAllowed
def self.matches?(request)
# use the request to do something
end
end
More information about securing the route here.
If you want to use resque-web then in order to fix this issue you will need to add in assets.rb:
Rails.application.config.assets.precompile += %w[idle lifebuoy poll rails working].map { |img| "resque_web/#{img}.png" }
Rails.application.config.assets.precompile += %w[resque_web/application.css]
Rails.application.config.assets.precompile += %w[resque_web/application.js]
Subsequently you will need to run: rake assets:precompile
And the issue should be fixed.

sitemap generator does not work properly / Rails Application

i built a simple sitemap with the gem sitemap generator.
It is supposed to list the job pages of my application.
E.g. www.mydomain.com/vacancies/24
There is a bug that I cannot fix myself for days:
It shows the wrong indexes (that do not exist). E.g.
www.mydomain.com/vacancies/645
enter code hereHere is my sitemap.rb file:
require 'rubygems'
require 'sitemap_generator'
SitemapGenerator::Sitemap.default_host = "http://www.frankfurtstartupjobs.com"
SitemapGenerator::Sitemap.create do
Vacancy.find_each do |vacancy|
add vacancy_path(vacancy), :lastmod => vacancy.updated_at, :priority => 0.5
end
end

Rails clockwork not running code

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
# ...

Cron job in ruby on rails not work

I followed the railscast http://railscasts.com/episodes/164-cron-in-ruby but I cant seem to make it worked.
I have schedule.rb in my config.
I wished to refresh my Database.count everyday in my homepage.
I cannot find the deploy.rb in my folder. Where is it?
For testing purpose, I changed it to every 2 seconds.
[schedule.rb]
every '2 * * * *' do
rake "pages_controller:home"
end
[pages_controller.rb]
class PagesController < ApplicationController
def home
#title = "Home"
#companies = Company.find(:all, :limit => 20)
#count = Company.count
end
I have put
gem 'whenever', :require => false
in my gemfile. What have gone missing?
I have used cron job but i run it as rake task please you can try it
every 3.minutes do
set :environment, 'development'
rake "daily",:output => {:error => 'error.log', :standard => 'cron.log'}
end
And my task is like
require 'rubygems'
task :daily => :environment do
puts "i am fine"
# do your code
end
If cron job run fine then there will be nothing in cron.log.Otherwise it will show you if
any error occurs and this file will be generate in your app root directory.
try it..
So... Couple things.
You need a Capfile and a config/deploy.rb to deploy your code using Capistrano and Whenver. You get this by running capify . ... You should most likely watch the railscast on capistrano deployment
Whenever is using configured like:
schedule.rb
every 1.day, :at => '4:30 am' do
runner 'Rails.cache.clear'
end
I really don't think rake "pages_controller:home is going to work unless this is something you've already created elsewhere. I'm further assuming you are caching this page, and that's why you need to refresh the cache.
Finally, you're setting your environment to development, which makes me think you are not deploying this, but instead just wanting to reset the cached home page... So just run rake cache:clear

How to configure an extra/different migrations folder

A colleague and I are working in different projects that share some models. So, we are sharing the models through a git submodule.
Additionally, we'd like to be able to also share migrations:
In this way, my colleague's migrations would be in the folder db/migrate/other_db of my project.
How can I configure rails migrations to also run the migrations in this extra folder?
In your config file (config/application.rb for all environments or config/environments/$(environment).rb only for particular environment) add this line:
config.paths['db/migrate'] += 'db/migrate/other_db'
And if you want to change default 'db/migrate' path (config.paths['db/migrate'] is an array with one string 'db/migrate' by default), do this:
config.paths['db/migrate'] = ['db/my_migrate']
Here are default config.paths, which we also can change:
"app" => ["app"],
"app/assets" => ["app/assets"],
"app/controllers" => ["app/controllers"],
"app/helpers" => ["app/helpers"],
"app/models" => ["app/models"],
"app/mailers" => ["app/mailers"],
"app/views" => ["app/views"],
"lib" => ["lib"],
"lib/assets" => ["lib/assets"],
"lib/tasks" => ["lib/tasks"],
"config" => ["config"],
"config/environments" => ["config/environments"],
"config/initializers" => ["config/initializers"],
"config/locales" => ["config/locales"],
"config/routes" => ["config/routes.rb"],
"db" => ["db"],
"db/migrate" => ["db/migrate"],
"db/seeds" => ["db/seeds.rb"],
"vendor" => ["vendor"],
"vendor/assets" => ["vendor/assets"],
"vendor/plugins" => ["vendor/plugins"],
"config/database" => ["config/database.yml"],
"config/environment" => ["config/environment.rb"],
"lib/templates" => ["lib/templates"],
"log" => ["log/development.log"],
"public" => ["public"],
"public/javascripts" => ["public/javascripts"],
"public/stylesheets" => ["public/stylesheets"],
"tmp" => ["tmp"],
Update for Rails 5/6;
Rails 5 recommends setting additional migration paths in your config/database.yml file. It's very easy, see this example;
development:
migrations_paths:
- "db/migrate/other_db"
- "db/migrate/something_else"
ActiveRecord::Migrator.migrations_path= will be deprecated in Rails 6.
based on the answer by Swanand, we can write a migration to load the migrations in an external dir:
class MigrateMetadata < ActiveRecord::Migration
MIGRATIONS_PATH='db/migrate/metadata'
def self.up
Dir["#{MIGRATIONS_PATH}/[0-9]*_*.rb"].
sort.map{|filename|require filename}.flatten.
each{|class_name| const_get(class_name).up}
end
def self.down
Dir["#{MIGRATIONS_PATH}/[0-9]*_*.rb"].sort.reverse.
map{|filename|require filename}.flatten.
each{|class_name| const_get(class_name).down}
end
end
By the way, if you are building a gem to work with Rails, you can place a block like the following in your rail tie to add the gem's own migrations.
root = ... # the path to your gem
initializer :append_migrations do |app|
unless app.root.to_s.match root
app.config.paths["db/migrate"] << File.join(root, 'db/migrate')
end
end
There is no need to copy migrations from your gem with generators if you use this technique.
You can make a method to yield the root directory of your gem with some thing like this...
module MyGemName
def root
File.expand_path '../..', __FILE__
end
module_method :root
end
... in the file lib/my_gem_name.rb in your gem.
I do not know of a very clean way to do it, but the code that runs migrations looks something like:
#migrations ||= begin
files = Dir["#{#migrations_path}/[0-9]*_*.rb"]
migrations = files.inject([]) do |klasses, file|
version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first
Where,
#migrations_path = 'db/migrate'
So if you change this to read from config file instead, it may work out in your favour. But as I said, this is definitely not a very clean way to do it.
Just add this initializer to your lib/engine.rb:
initializer 'your_engine_name.migrations' do |app|
config.paths['db/migrate'].expanded.each do |expanded_path|
app.config.paths['db/migrate'] << expanded_path
ActiveRecord::Migrator.migrations_paths << expanded_path
if Rake.application.top_level_tasks.empty?
ActiveRecord::Migration.check_pending! if ActiveRecord::Migrator.needs_migration?
end
end
end

Resources