views:

25

answers:

1

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.

+1  A: 

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.

Simone Carletti
Perfect! I didn't know the lib files weren't autoloaded. The reason I tried Sinatra is because I read regular routes perform slower than middleware but I guess right now whatever works is fine since I'm still new.Also it needs to send 301 headers so it seems more difficult in regular routes.