Accessing Named Routes in Rakefile - ruby-on-rails

I have some named routes like this rake routes:
birthdays GET /birthdays(.:format) birthdays#index
In a rakefile I simply want to be able to call birthdays_url like I would in my view.
task :import_birthdays => :environment do
url = birthdays_url
end
But I'm getting an error undefined local variable or method 'birthdays_url' for main:Object

You can either use this example code in your rake task:
include Rails.application.routes.url_helpers
puts birthdays_url(:host => 'example.com')
or you can use this example code in your rake task:
puts Rails.application.routes.url_helpers.birthdays_url(:host => 'example.com')
If you only want the path part of the URL, you can use (:only_path => true) instead of (:host => 'example.com'). So, that would give you just /birthdays instead of http://example.com/birthdays.
You need either the (:host => 'example.com') or (:only_path => true) piece, because the rake task doesn't know that bit of information and will give this error without it:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true

for Rails 4 include code with your domain at the top of your rake task
include Rails.application.routes.url_helpers
default_url_options[:host] = 'example.com'

use this:
Rails.application.routes.url_helpers.birthdays_url
or to be less verbose:
include Rails.application.routes.url_helpers
url = birthdays_url

Related

Cannot connect to the model by rails sitemap_generator gem?

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)

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

Calling a Rake task from a controller with parameters

I have a instance variable in my controller that queries my table for a value, and I need to send that value through to my rake task.
So here are the 2 relevant lines in my controller:
#turl = Fteam.where(:id => #ids).select(:TeamUrl)
system "rake updateTm:update[#turl]"
Here is my rake file:
desc "Import Players"
task :update, [:Tmurl] => :environment do |t, args|
require 'rubygems'
require 'nokogiri'
require 'open-uri'
require 'mechanize'
agent = Mechanize.new
puts "This is the selected Team URL: #{args.Tmurl}"
end
end
This is what the rake task returns:
This is the selected Team URL: #turl
My guess is that the controller is not passing the variable correctly. So how can I pass the actual value of the variable to the rake task so the output is correct?
probably you may need to use interpolation
"rake updateTm:update[#{#turl}]"
It is a .rb file. and and in ruby we can'y call variable directly.
You should use
system "rake updateTm:update[#{#turl}]"
Thanks for all the help and ideas everyone, i came up with a solution.
Instead of #turl = Fteam.where(:id => #ids).select(:TeamUrl)
I changed that to #turl = Fteam.where(:id => #ids).pluck(:TeamUrl)
That gave me the actual value that i needed rather then the active record, which was causing the error because it could not pass a value for which it could not translate or understand.

Rails 3 + Exception notifier: How do I use exception notifier for rake tasks?

So in the old plugin for Rails 2 there used to be a method called notifiable that I could use to surround whatever Rake task I needed to attach exception notifier to. However, when I try to run my rake task it gives me an undefined method error. I looked around and noticed someone else use the exception_notify method and tried replacing this:
task(:create_orders_for => :environment) do
notifiable do
...
end
end
with this:
exception_notify {:create_orders_for => :environment} do
#notifiable do
...
end
But it doesn't work. Does anyone know what the Rails 3 version of this method is? I can't find it anywhere.
So this is what I eventually ended up doing. Works great.
Add the middleware configuration to your environment/whatever_environment_you_want.rb file
If you're testing in dev or test, you need to set the consider_all_requests_local to false
Change your rake task to this:
task(:create_orders_for => :environment) do
begin
...
rescue => e
ExceptionNotifier::Notifier.exception_notification(Rails.env, e).deliver
end
end

Accessing a Model from js.erb file when precompiling

I have a Rails 3 app where I am trying to populate a javascript variable with every Nation in my database (less than 300 nations) as a JSON object. This is the relevant line in my nations.js.erb file:
_this.nations = <%= Nation.all.to_json :only => [:id], :methods => :text %>;
When I call my js file in a browser, /assets/users.js which does a require of the nations file, the _this.nations variable is populated perfectly. When I try to do a precompile I get the following:
$> rake assets:precompile
$> rake aborted!
uninitialized constant Nation (in nations.js.erb)
So my question is this: is it possible to reference the Nation model, or any model, from within the js.erb file for precompiling? I also tried using my NationsHelper but my error just changed to uninitialized constant NationsHelper.
I'm fairly new to RoR so if relevant information is needed that I haven't provided, please just ask.
If you have config.assets.initialize_on_precompile set to false somewhere then try enabling it
config.assets.initialize_on_precompile = true
Why don't you make the call in the controller
controller
#nations = Nation.all
nations.js.erb
_this.nations = <%= #nations.all.to_json :only => [:id], :methods => :text %>;

Resources