RoR:Sinatra 产生错误“未初始化常量”

发布于 2024-09-28 05:13:00 字数 771 浏览 0 评论 0原文

我是 Ruby on Rails 的新手(以前和现在是 PHP 专家),所以请原谅我的无知,但我试图让 Sinatra 作为中间件来重定向一些旧的 url,因为我尝试了 gemrack-rewrite 并且无法将其实现工作也可以。

我正在使用 ASCIIcast 中的代码示例,因此在我的 paths.rb 中以下内容:

root :to => HomeApp

(^我重定向根仅用于测试)

在我的 lib 文件夹中,我有 home_app.rb

class HomeApp < Sinatra::Base  
  get "/" do  
    "Hello from Sinatra"  
  end  
end 

当我启动服务器(或者如果它已经在运行)时,它会产生错误:

routes.rb:10: uninitialized constant HomeApp

这似乎是它不知道lib/home_app.rb 文件的。

我已将 Sinatra 包含在我的 Gemfile 中并运行捆绑安装并确认它已包含在内。

我只想将旧网址从旧网站重新路由到新的 ruby​​ 应用程序,但无法让任何中间件/机架内容正常工作。所有文档都假设您不是一个完全的新手或者是 RoR 3.0 之前的新手。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

踏雪无痕 2024-10-05 05:13:00

如果您想重定向某些 URL,则无需使用 Sinatra。您可以使用新的redirect 方法。请参阅 Rails Dispatch 文章

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

在您的具体情况下,问题是 HomeApp 类未加载。将 /lib 文件夹添加到加载路径,更改 application.rb

config.autoload_paths += %W( #{config.root}/lib )

require 文件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文