开发者

RoR: Sinatra producing error "uninitialized constant"

开发者 https://www.devze.com 2023-01-20 03:47 出处:网络
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

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 in开发者_如何转开发cluded.

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消