Resque front-end doesn't load in Rails - ruby-on-rails-3

I've set up the Resque-web interface to work with my rails 3 app but it doesn't work in either development or production environments. My set up is as follows, and going to localhost:3000/resque just gives me the 404 page I've set up.
Routes.rb
mount Resque::Server.new, :at => "/resque"
Resque initializer
require 'resque'
require 'resque/server'
if Rails.env.development?
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => 'localhost', :port => '6379')
else
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
Dir["#{Rails.root.to_s}/app/jobs/*.rb"].each { |file| require file }
Resque::Server.class_eval do
use Rack::Auth::Basic do |email, password|
user = User.authenticate( 'foo#bar.co.za', 'password' )
user && user.admin?
end
end

My routes.rb file has:
require "resque/server"
MyApp::Application.routes.draw do
# routes and stuff
mount Resque::Server.new, :at => "/resque"
root :to => 'page#welcome'
end
Maybe you forgot the require inside the routes.rb file?
I do not have a resque initializer, so I don't know if that is causing your problem or not.

I have a route that matches '/:name' to a 'model#index' route must be confusing Resque so I've just mounted it to '/resque/web'

Related

Adding HTTP Basic Authentication to some routes in routes.rb

I want to add HTTP Basic Authentication to a some of my routes in my routes.rb file.
I got the idea from http://asciicasts.com/episodes/271-resque
routes.rb
Coderbits::Application.routes.draw do
resources :snippets
root :to => "snippets#new"
authenticate :admin do
mount Resque::Server, :at => "/resque"
end
end
config/initializers/resque_auth.rb
Resque::Server.use(Rack::Auth::Basic) do |user, password|
password == "secret"
end
If I just want to protect routes that are in my rails app, what should I put in the initializer file?
My work around for right now is to add a before filter in my application controller using if the request is not for a controller that I've whitelisted:
authenticate_or_request_with_http_basic do |user, password|
user == ENV["ADMIN_USER"] && password == ENV["ADMIN_PASS"]
end
I just put
http_basic_authenticate_with :name => "admin", :password => "secret"
inside my controller

How do I configure WEBrick to use an intermediate certificate with HTTPS?

I am currently using the following options in my Rails app to enable HTTPS with WEBrick:
{
:Port => 3000,
:environment => (ENV['RAILS_ENV'] || "development").dup,
:daemonize => false,
:debugger => false,
:pid => File.expand_path("tmp/pids/server.pid"),
:config => File.expand_path("config.ru"),
:SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLPrivateKey => OpenSSL::PKey::RSA.new(
File.open("certificates/https/key.pem").read),
:SSLCertificate => OpenSSL::X509::Certificate.new(
File.open("certificates/https/cert.pem").read),
:SSLCertName => [["CN", WEBrick::Utils::getservername]]
}
How would I go about specifying an intermediate certificate?
I managed to find an answer after an extra hour of googling for keywords. Here is the option to define an intermediate certificate:
:SSLExtraChainCert => [
OpenSSL::X509::Certificate.new(
File.open("certificates/intermediate.crt").read)]
Note that the option requires an Array object, allowing to you include multiple certificates if needed.
If you are using rails 3, then modify the script/rails file as
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
require 'rubygems' # if ruby 1.8.7
require 'rails/commands/server'
require 'rack'
require 'webrick'
require 'webrick/https'
module Rails
class Server < ::Rack::Server
def default_options
super.merge({
:Port => 3000,
:environment => (ENV['RAILS_ENV'] || "development").dup,
:daemonize => false,
:debugger => false,
:pid => File.expand_path("tmp/pids/server.pid"),
:config => File.expand_path("config.ru"),
:SSLEnable => true,
:SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
:SSLPrivateKey => OpenSSL::PKey::RSA.new(
File.open("/key/vhost1.key").read),
:SSLCertificate => OpenSSL::X509::Certificate.new(
File.open("/crt/vhost1.crt").read),
:SSLCertName => [["CN", WEBrick::Utils::getservername]],
})
end
end
end
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands'
The above code was modified from the example in Configuring WEBrick to use SSL in Rails 3. This worked for me.

migrated rails3 app to heroku, now paperclip+s3 not uploading files - nothing in heroku logs

Hi I just migrated to heroku cedar stack. app is Rails 3, ive been using paperclip on s3 just fine previously. my gemfile has:
gem 'paperclip', '2.3.11'
gem 'aws-s3', '0.6.2'
my model file has:
class UserProfile < ActiveRecord::Base
has_attached_file :avatar,
:styles => { :thumb => "150x200#" },
:default_style => :thumb,
:default_url => "missingAvatar.png",
:storage => :s3,
:s3_credentials => S3_CREDENTIALS
Ive created a new file to store S3_CREDENTIALS,:
# initializers/s3.rb
if Rails.env == "production"
# set credentials from ENV hash
S3_CREDENTIALS = { :access_key_id => ENV['S3_KEY'], :secret_access_key => ENV['S3_SECRET'], :bucket => "app_content"}
else
# get credentials from YML file
S3_CREDENTIALS = Rails.root.join("config/s3.yml")
end
... with s3.yml containing my keys for local dev, and the keys set in heroku config:
S3_KEY => AK...
S3_SECRET => FFE...
as mentioned, everything works just fine on local. i can even see the existing avatars from before. just, when i try to upload anything new, i get no errors in heroku logs, but the picture never uploads.
ive went thru many stackoverflow issues, but none matching this. can anyone help??
Try adding the following to your model
class UserProfile < ActiveRecord::Base
has_attached_file :avatar,
:styles => { :thumb => "150x200#" },
:default_style => :thumb,
:default_url => "missingAvatar.png",
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:url => "/assets/avatar/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/avatar/:id/:style/:basename.:extension"
The missing path / default path might be causing an issue.
turns out i needed to upgrade my paperclip gem to '2.4.5'
i did this in my Gemfile, then bundle update, and it worked!

Rails url_for Gravatar routing error

This function is defined in the application_help.rb:
def gravatar_url_for(email, options = {})
url_for(
{
:protocol => 'http://',
:host => 'www.gravatar.com',
:controller => 'avatar',
# :controller => 'avatar.php',
:gravatar_id => Digest::MD5.hexdigest(email),
:only_path => false
}.merge(options)
)
end
It's used in views:
<%= image_tag(gravatar_url_for user.email, {:d => 'identicon', :s => 32, :r => 'g'}) %>
Occasionally, its usage will result in a routing error:
No route matches {:controller=>"avatar", :d=>"identicon", :r=>"g", :gravatar_id=>"486575e581db04b7c8ca218af8488657", :s=>32}
A valid email is being supplied when the error occurs.
If I replace the url_for() with this logic, it works as expected:
url_for("http://www.gravatar.com/avatar/" + Digest::MD5.hexdigest(email) + "?d=identicon&s=40&r=g")
** edit **
I had removed the following line from the routes.rb file:
# This is a legacy wild controller route that's not recommended for RESTful applications.
# Note: This route will make all actions in every controller accessible via GET requests.
match ':controller(/:action(/:id(.:format)))'
Is there a way to get the url_for to work without the 'legacy wild controller route'?
You might want to take a look at the Gravtastic plugin for Rails which supports Gravatar images in both Ruby and JavaScript.

Using Rails 3 gem Manifesto

I am trying to use the HTML5 manifest function using a gem called Manifesto. I am stuck on the instructions for usage. I cannot figure out where those settings are supposed to go.
Any ideas? Perhaps a better gem?
https://github.com/johntopley/manifesto#readme
Thankful for all help!
You can put you settings in a file under config/initializers/. Use an informational name (like manifesto.rb). However you don't need a config with the basic usage.
In your Gemfile file, add:
gem 'manifesto'
then install via bundle:
bundle install
create the file app/controllers/manifest_controller.rb
class ManifestController < ApplicationController
def show
headers['Content-Type'] = 'text/cache-manifest'
render :text => Manifesto.cache, :layout => false
end
end
in config/routes.rb add:
match '/manifest' => 'manifest#show'
Restart your app and view the result at http://localhost:3000/manifest
You can pass the option directly to Manifesto.cache like:
# change
render :text => Manifesto.cache, :layout => false
# to
render :text => Manifesto.cache(:directory => './mobile', :compute_hash => false), :layout => false
Or use a YAML file and an initializer.
The config/manifesto.yaml file:
# directory is relative to Rails root
directory: './mobile'
compute_hash: false
The config/initializers/manifesto.rb file:
# Load the config file and convert keys from strings in symbols (the manifesto gem need symbolized options).
MANIFESTO_CONFIG = YAML.load_file(Rails.root.join('config', 'manifesto.yml').to_s).inject({}){|config,(k,v)| config[k.to_sym] = v; config}
And pass the loaded config to Manifesto.cache like:
# change
render :text => Manifesto.cache, :layout => false
# to
render :text => Manifesto.cache(MANIFESTO_CONFIG), :layout => false