Using rack-proxy with Rails to access Faye - ruby-on-rails

I have a rails app which provides real-time functionality through Faye. My clients are going to access the Faye server through example.com:9292/faye. Instead of using that address and revealing my machine's open ports, I am trying to add a rack middleware and by using 'rack-proxy' gem, proxy my example.com/faye requests to example.com:9292/faye. My Rails middleware code looks like this:
class FayeProxy < Rack::Proxy
def rewrite_env(env)
request = Rack::Request.new(env)
if request.path =~ %r{^/faye}
env["HTTP_HOST"] = "localhost:9292"
end
env
end
end
Also I added the middleware to config/application.rb by config.middleware.use "FayeProxy", but when I run my rails server I get the following error:
/gems/ruby-1.9.3-p194/gems/rack-proxy-0.5.0/lib/rack/proxy.rb:12:in 'initialize': undefined method `key?' for # (NoMethodError)
and even if I remove meta_request gem I will get
*/gems/ruby-1.9.3-p194/gems/rack-proxy-0.5.1/lib/rack/proxy.rb:12:in initialize': undefined methodkey?' for # (NoMethodError)
*
Any help is really appreciated if anybody has experienced this before or knows the solution.
P.s. I'm using Rails 3.2.13, rack 1.4.5, rack-proxy 0.5.1.

It seems that the rack proxy shall not be treated as middleware, rather it should be mounted via routes as described here: http://inductor.induktiv.at/blog/2010/05/23/mount-rack-apps-in-rails-3/.

I know this is old, but I was just having a similar issue, but I suspect that the folder you faye_proxy.rb is in is not included in rails by default.
Adding an initializer maybe ./config/initializers/proxy.rb which contains
require "#{Rails.root}/lib/faye_proxy.rb"
Replace lib with wherever you put the proxy.

Related

Rails 4.1 with ActiveJob gem - uninitialized constant ActiveJob

I have followed the getting started with active job article by EngineYard. The article states:
You'll need Rails 4.2.0beta1 or greater if you want to Active Job available by default (in older versions of Rails, you can require it as a gem)
I am trying to use ActiveJob in my Rails 4.1 project. I added ActiveJob to my gemfile gem 'activejob'. As per the article, I have:
#config/initializers/active_job.rb
ActiveJob::Base.queue_adapter = :resque
However, when I run rails server I get the following error:
config/initializers/active_job.rb:1:in': uninitialized constant ActiveJob (NameError)`
EDIT - Fixed typo "gem active job"
UPDATE 1
Following solydest's suggestion below, adding require 'active_job' to application.rb allows me to no longer get the uninitialized constant ActiveJob error but instead I receive the error undefined method perform_later' when I try to call my job. I am following the edge rails guide and enqueue my job with code similar to:
MyJob.perform_later(record)
The beginning of my job class:
class MyJob < ActiveJob::Base
queue_as :images
def perform(id)
I added require 'active_job' to config/application.rb just below all the other require directives and that solved the issue for me.
I've found that the gem version of ActiveJob for rails 4.1 is 0 which is different from the version in Rails 4.2.
If you're using the v0 of ActiveJob, the syntax should be :
MyJob.enqueue(record)
or
MyJob.enqueue(record, options)
You will find a lot of interesting things about ActiveJob with Rails 4.1 in this article :
http://kinopyo.com/blog/use-activejob-in-rails-4-1/
Use this gem if you want to use Active Job on Rails 4.0 or 4.1:
https://github.com/ankane/activejob_backport
But this gem did not pull Action Mailer in, you need to stick with deliver, see https://github.com/ankane/activejob_backport/issues/1.

How to change the default rails server in Rails 3?

I'm new to Rails and I'm wondering if there is an option to change the default rails server, i.e., webrick, for another one such as 'puma' or 'thin'. I know it is possible to specify which server to run with 'rails server' command, however I would like to use this command without specify the name of the server so it can run the default rails server. Is there a way to change the default rails server into a configuration file or something like this? Thanks in advance for your help!
Based on James Hebden's answer:
Add Puma to gemfile
# Gemfile
gem 'puma'
Bundle install it
bundle
Make it default, paste this code into script/rails above require 'rails/commands':
require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler.get(:puma)
So script/rails (in Rails 3.2.12) will look like:
#!/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.
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler.get(:puma)
require 'rails/commands'
Run server
rails s
=> Booting Puma
Rack (the interface between rails and a web server) has handlers for the default WEBrick, and also for Thin. If you place the following in your Gemfile in the root of your rails project
gem 'thin'
rails server will automatically use Thin. This has been the case since 3.2rc2.
This unfortunately only applies to Thin, as Rack does not have built-in support for Unicorn, and others.
For servers that have Rack handlers (again, sadly Unicorn does not), you can do a bit of a hack to get rails server to use them. In your scripts/rails file in the root of your rails project, you can add the below just above `require 'rails/commands'
require 'rack/handler'
Rack::Handler::WEBrick = Rack::Handler::<name of handler class>
This essentially resets the handler for WEBrick to point to the handler for the server you would like to use.
To get an understanding of the supported Rack handlers, take a look at the comments in the source: https://github.com/rkh/rack/blob/master/lib/rack/handler.rb
I think rails simply passes on the server option provided to rack. Rack has the following logic to determine what server to run:
https://github.com/rack/rack/blob/master/lib/rack/server.rb#L271-L273
def server
#_server ||= Rack::Handler.get(options[:server]) || Rack::Handler.default(options)
end
The first case is when a :server option was passed to the rails server command. The second is to determine the default. It looks like:
https://github.com/rack/rack/blob/master/lib/rack/handler.rb#L46-L59
def self.default(options = {})
# Guess.
if ENV.include?("PHP_FCGI_CHILDREN")
# We already speak FastCGI
options.delete :File
options.delete :Port
Rack::Handler::FastCGI
elsif ENV.include?("REQUEST_METHOD")
Rack::Handler::CGI
else
pick ['thin', 'puma', 'webrick']
end
end
Thin and Puma should be automatically picked up. The fallback is Webrick. Of course other web servers could override this behavior to make them the first in the chain.
If your Webserver is not picked up by default you could monkey-patch the default method to work like you want it. Of course this could break in future versions of rack.
Rack will now look at a RACK_HANDLER environment variable file to see if you've specified a default rack handler. You can add a line like this to your .env file to set the default if you're using dotenv, or specify the assignment from the command line.
`RACK_HANDLER=webrick`
This should work as of this pull request:
https://github.com/rack/rack/pull/590
I wouldn't get hung up on specifically using the rails server command. Just install whichever gem you want and alias the command (e.g. rails s Puma) to something simple like rs.
If you want unicorn/thin/etc, just add the gem to your gemfile
i.e. gem 'unicorn', gem 'thin', etc. then run bundle install at the command line.
As far as I can tell, adding either of these gems runs the appropriate server via rails server
UPDATE
Apparently this only works for Thin or Puma.
If you have thin in your Gemfile, you need to do this:
require 'rack/handler'
Rack::Handler::Thin = Rack::Handler.get(:puma)
If you use bash run: export RACK_HANDLER=webrick

Rails sitemap_generator Uninitialized Constant?

I'm trying to use the Rails site map_generator gem to generate site maps for a 8,000,00 page site. The gem can be found here: https://github.com/kjvarga/sitemap_generator
Here is my code in sitemap.rb:
require 'rubygems'
require 'sitemap_generator'
# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = "http://www.mysite.com"
SitemapGenerator::Sitemap.create do
add '/content.aspx?page=privacypolicy'
Product.find_each do |product|
add product_path(ppid), :lastmod => content.updated_at
end
end
However, when I run
>> ruby sitemap.rb
I get an error that says:
sitemap.rb:9:in `block in ': uninitialized constant
SitemapGenerator::Interpreter::Product (NameError)
However "Product" is the correct name of my model. Why is this happening?
I'm running Rails 3.1.2 and Ruby 1.9.
I'm the author of the gem. Better to open an issue on the GitHub page in future. SitemapGenerator does work in Rails 3 and Ruby 1.9.*. If you are running Rails, you don't need these lines:
require 'rubygems'
require 'sitemap_generator'
Also you generate your sitemaps by running Rake:
rake sitemap:refresh:no_ping
What is happening in your case is that because you're not running through Rake, the script does not know about the Product class, since your Rails environment hasn't been loaded.
Well, I wasn't able to get this gem working. My guess is that it doesn't work on Rails 3.1.2 or with Ruby 1.9. However, I was able to get another gem (big_sitemap) to work. Here is the link to it.
https://github.com/alexrabarts/big_sitemap

Incompatability between Rails 2.3.5 and Omniauth

I use Rails 2.3.5 and want to use Omniauth however I can't get them to work together, as rails 2.3.5 requies Rack 1.0.1 and Omniauth (version 0.1.6) requires Rack 1.1
I deploy on Heroku so I don't believe I can hack into Rails and remove the hard dependency on version 1.0.1 of Rack.
Any help very much appreciated.
Paul
We had a similar issue. We were on Rails 2.3.4 and wanted to use OmniAuth (0.2.6). Unfortunately the only possible solution I've found so far is to upgrade to Rails 2.3.8 or later which runs on Rack 1.1 (the minimum required by OmniAuth) and then require OmniAuth like so:
# In config/environment.rb require 'omniauth' (or 'oa-<strategy_name>') before Rails::Initialize
require 'omniauth'
Rails::Initializer.run do |config|
...
# Add your own initializer for OmniAuth
# /config/initializers/omniauth.rb
ActionController::Dispatcher.middleware.use OmniAuth::Builder do
# your strategy provider logic
end
This was mostly groomed from this thread / links in it: http://groups.google.com/group/omniauth/browse_thread/thread/676fa835428e9c83
Unfortunately I'm in the middle of all of this right now so I can't promise this works fully as I'm using a custom strategy and haven't quite made it all the way to the end yet. Hopefully it provides some starting points for you to dig deeper if you're still stuck on this if nothing else.

Rails 2.3.2 trying to render ERB instead of HAML

Rails is suddenly trying to render ERB instead of Haml and I can't figure out why. I've created new rails projects, reinstalled Haml, and reinstalled Rails.
Here's exactly the steps I take when making my application (Rails 2.3.2):
rails> rails test
rails> cd test
rails\test> haml --rails .
rails\test> ruby script\generate model user email:string password:string
rails\test> ruby script\generate controller users index
rails\test> rake db:migrate
Here's what the UsersController looks like:
class UsersController < ApplicationController
def index
#users = User.all
end
end
My routes:
ActionController::Routing::Routes.draw do |map|
map.resources :users
end
I now create views\users\index.html.haml:
%table
%th(style="text-align: left;")
%h1 Users
- for user in #users
%tr
%td= user.email
%td= user.password
Annnd run the server...
I navigate to localhost:3000\users and I get this error message:
Template is missing
Missing template users/index.erb in view path app/views
For some reason Rails is trying to find and render .erb files instead of .haml files.
vendor\plugins\haml\init.rb exists, untouched.
I've reinstalled Haml (Pretty Penny) multiple times and still get the same results.
I've also tried adding config.gem 'haml' to my environment.rb but this also doesn't work.
I can't figure out why suddenly rails will not render haml for me.
Hi it seem like haml is not enabled as Rails plugin ,in order to enable it use the following command .
Go to your application folder from the command prompt type the following
$ cd ..
$ haml --rails <yourproject>
if this doesnot work try installing haml gem with the following code
$ gem install haml
I tried with above example , it did work for me ,i have haml gem installed in my ubuntu system .
Good luck !
NOTE: "haml --rails" was deprecated in HAML 3.1
It's worth noting that the fact that the error message says that it couldn't find index.erb doesn't mean that it didn't look for index.haml too. The erb extension is hard-coded into the error message.
I thought that I had the same problem you describe, but it turned out that my application simply couldn't find my partial at all - it had nothing to do with the file extension.
I had this same problem (see this post) with Rails 2.3.4. Multiple gem uninstall/gem install rails didn't fix the problem. But downgrading to Rails 2.3.2 worked! (I know HAML previously worked in this project with this version of Rails).
sudo gem install -v 2.3.2 rails
Using Rails 3.1, I ran in to the same error and had to restart the web server.
I have this old project in rails 2.3.18, where the gems are managed using bundler, and all I had to do was explicitly use version 3.1.3 It did not work with the latest version (4.0). So in my version I added
gem 'haml', '3.1.3'
did bundle install and restarted my development server :)
Hmmm strange, this might be related.
According to: http://www.ruby-forum.com/topic/101346 you should use resource_url helpers in controllers and resource_path helpers in views. Right?
BUT, if I do use a resource_url helper in a redirect_to call inside my controller, then I get:
Missing template htp://localhost:4000/categories/new.erb in view path app/views
If I use the resource_path helper instead, there aren't any problems at all.
Anyone knows what could be wrong?
Why is the resource_url helper trying to redirect to an .erb file?
This is the error from the server log:
ActionView::MissingTemplate (Missing template http://localhost:4000/categories/new.erb in view path app/views):
haml (2.2.2) lib/haml/helpers/action_view_mods.rb:13:in `render'
app/controllers/categories_controller.rb:15:in `create'
haml (2.2.2) rails/./lib/sass/plugin/rails.rb:19:in `process'
P.S. This is in Rails 2.3.3
maybe your file name is wrong, if you have a whitespace in the end of the index.html.haml_, rails will wrong...
I ran into the same problem and I had to restart my server after installing Haml before my rails app recognized the changes.
I was having this issue with Ruby 1.9x, Rails 2.3.5, and HAML 3.1. I believe part of the issue is that some of the deprecated calls in 1.8 were removed in 1.9.
IMHO, if you want to use HAML in Rails 2, you'd be better off downgrading to Ruby 1.8. (which is what I did to fix my problem). In Rails 2, you MUST have the gem.config "haml" in your config.
Even better, move forward to Rails 3 on Ruby 1.9!
I've had this same problem.
The solution is documented in https://github.com/haml/haml/issues/672
You need to add
config.after_initialize do
require 'haml'
Haml.init_rails(binding)
end
inside your config/environment.rb within the Rails::Initializer.run do |config| configuration block !!
Updated - Not actual anymore:
I name all of my haml file only .haml
To illustrate:
test.haml
# not
test.html.haml
Update 5 years later:
I recommend to name them "file.format.haml", because its much more clear which format is the outcome...

Resources