Rails 3 与 Rails 2 路由问题

发布于 2024-11-03 15:38:12 字数 873 浏览 1 评论 0 原文

我一直在关注教程@ Noupe 的目的学习 Rails 的过程。一切进展顺利,应用程序也能运行,但我猜它是为 Rails 2 编写的,因此使用了一些已弃用的代码片段。

虽然我已经解决了大部分问题,但我还剩下一个关于路由的问题。本教程中的 Rails 2 代码为:

ActionController::Routing::Routes.draw do |map|
地图.资源:帖子
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end

这工作正常,但它是旧代码。我尝试重写代码如下:

Twitter::Application.routes do
资源:帖子
匹配 ':controller/:action/:id'
匹配 ':controller/:action/:id.:format'
当我尝试访问 http://localhost:3000/posts 时,这会导致

错误 -

路由错误 没有路线匹配“/posts”

谁能建议我哪里出错了?我确实注意到,如果我在第一行(而不是 Twitter)中写入 twitter,则会出现编译器错误 - 因为我的应用程序名为 twitter,所以我认为这可能是问题所在,但似乎并非如此。

非常感谢任何帮助!

I have been following a tutorial @ Noupe with the intention of learning Rails. It's going well and the app works, but I gather it was written for Rails 2, so there are some deprecated pieces of code used.

While I have resolved most of these, I have one remaining problem with the routing. The Rails 2 code in the tutorial is:

ActionController::Routing::Routes.draw do |map|
map.resources :posts
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end

This works fine, but it is the old code. I have attempted to rewrite the code as follows:

Twitter::Application.routes do
resources :posts
match ':controller/:action/:id'
match ':controller/:action/:id.:format'
end

This results in an error when I attempt to visit http://localhost:3000/posts -

Routing Error
No route matches "/posts"

Can anyone suggest where I am going wrong? I did notice that I get a compiler error if I write twitter in the first line (instead of Twitter) - since my app is named twitter, I thought this might be the issue, but it does not seem to be.

Any help greatly appreciated!

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

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

发布评论

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

评论(1

雪若未夕 2024-11-10 15:38:12

您似乎错过了对绘制方法的调用。尝试将第一行更改为以下内容:

Twitter::Application.routes.draw do

It seems like you are missing the call to the method draw. Try to change the first line to the following:

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