Ruby on rails 在Ruby on Rails中路由生成的路径

Ruby on rails 在Ruby on Rails中路由生成的路径,ruby-on-rails,routing,Ruby On Rails,Routing,我是ruby on rails的初学者,我在最后一个小时尝试做以下事情: 我有一个RubyonRails应用程序——包含帖子和分类的博客。 我想有另一个帖子的URL(我想有http://localhost:3000/news而不是http://localhost:3000/posts)首先,我试图将控制器和类从帖子替换为新闻,但我放弃了(因为令人恼火的单数复数)。然后在我的示例中,我将map.resources:posts(案例1)替换为 或 在我在上看到的routes.rb中。它也不起作用 我

我是ruby on rails的初学者,我在最后一个小时尝试做以下事情:

我有一个RubyonRails应用程序——包含帖子和分类的博客。 我想有另一个帖子的URL(我想有
http://localhost:3000/news
而不是
http://localhost:3000/posts
)首先,我试图将控制器和类从
帖子
替换为
新闻
,但我放弃了(因为令人恼火的单数复数)。然后在我的示例中,我将
map.resources:posts
(案例1)替换为

在我在上看到的
routes.rb中。它也不起作用

我该怎么做


编辑:

rake路由的输出为(仅第一行):

对于案例1和案例3:

               posts GET    /posts                           {:action=>"index", :controller=>"posts"}
     formatted_posts GET    /posts.:format                   {:action=>"index", :controller=>"posts"}
                     POST   /posts                           {:action=>"create", :controller=>"posts"}
                     POST   /posts.:format                   {:action=>"create", :controller=>"posts"}
            new_post GET    /posts/new                       {:action=>"new", :controller=>"posts"}
  formatted_new_post GET    /posts/new.:format               {:action=>"new", :controller=>"posts"}
           edit_post GET    /posts/:id/edit                  {:action=>"edit", :controller=>"posts"}
 formatted_edit_post GET    /posts/:id/edit.:format          {:action=>"edit", :controller=>"posts"}
                post GET    /posts/:id                       {:action=>"show", :controller=>"posts"}
      formatted_post GET    /posts/:id.:format               {:action=>"show", :controller=>"posts"}
                     PUT    /posts/:id                       {:action=>"update", :controller=>"posts"}
                     PUT    /posts/:id.:format               {:action=>"update", :controller=>"posts"}
                     DELETE /posts/:id                       {:action=>"destroy", :controller=>"posts"}
                     DELETE /posts/:id.:format               {:action=>"destroy", :controller=>"posts"}
案例2的输出:

           news_index GET    /news                            {:action=>"index", :controller=>"posts"}
 formatted_news_index GET    /news.:format                    {:action=>"index", :controller=>"posts"}
                      POST   /news                            {:action=>"create", :controller=>"posts"}
                      POST   /news.:format                    {:action=>"create", :controller=>"posts"}
             new_news GET    /news/new                        {:action=>"new", :controller=>"posts"}
   formatted_new_news GET    /news/new.:format                {:action=>"new", :controller=>"posts"}
            edit_news GET    /news/:id/edit                   {:action=>"edit", :controller=>"posts"}
  formatted_edit_news GET    /news/:id/edit.:format           {:action=>"edit", :controller=>"posts"}
                 news GET    /news/:id                        {:action=>"show", :controller=>"posts"}
       formatted_news GET    /news/:id.:format                {:action=>"show", :controller=>"posts"}
                      PUT    /news/:id                        {:action=>"update", :controller=>"posts"}
                      PUT    /news/:id.:format                {:action=>"update", :controller=>"posts"}
                      DELETE /news/:id                        {:action=>"destroy", :controller=>"posts"}
                      DELETE /news/:id.:format                {:action=>"destroy", :controller=>"posts"}

案例2中我有错误,因为在我的源代码中我没有编辑新闻,例如,我有

您的第一步很好:将所有帖子*类替换为新闻*类。调用模型新闻和控制器新闻控制器应该不会有问题。确保在代码中找到所有出现的内容(同样,类似于
post\u path
的内容也应替换为
news\u path

接下来,将您的路线修改为

map.resources :news
以便使用NewsController而不是PostsController。它应该会起作用


注意:不要忘记重新启动您的Web服务器。

您的第一步很好:将所有Post)*类替换为News*类。调用模型新闻和控制器新闻控制器应该不会有问题。确保在代码中找到所有出现的内容(同样,类似于
post\u path
的内容也应替换为
news\u path

接下来,将您的路线修改为

map.resources :news
以便使用NewsController而不是PostsController。它应该会起作用

注意:不要忘记重新启动您的Web服务器。

单词“news”在单数(成员)和复数(集合)名称之间存在歧义。您可以尝试解决这个问题,但最终可能会混淆您自己和rails。(是“news_path”需要成员的id参数,还是集合路径?什么是“新闻”?)

让我们坚持称他们为博文:

map.resources :posts, :as => "news", :singular => "news"
  • 您的路线将是“/新闻”
  • 您的控制器将是PostsController
  • 您的路径助手将是posts\u path、post\u path、edit\u post\u path等
换句话说,在所有情况下,您都会将资源称为“posts”,但它们会出现在“/news/*”下的路径中。

单词“news”在单数(成员)和复数(集合)名称之间存在歧义。您可以尝试解决这个问题,但最终可能会混淆您自己和rails。(是“news_path”需要成员的id参数,还是集合路径?什么是“新闻”?)

让我们坚持称他们为博文:

map.resources :posts, :as => "news", :singular => "news"
  • 您的路线将是“/新闻”
  • 您的控制器将是PostsController
  • 您的路径助手将是posts\u path、post\u path、edit\u post\u path等

换句话说,在所有情况下,您都会调用资源“posts”,但它们将显示在“/news/*”下的路径中。您可以添加
rake routes
的输出吗?当您执行
映射时会发生什么。资源:posts,:as=>“news”
并尝试访问
/news
?@Marcel,我添加了输出@Alex,在这种情况下,我得到了
路由错误-没有路由与“{:method=>:get}
匹配“/news”,但它可以与
posts
.Hm一起工作,好的。在我看来,一切都应该正常。您是否也可以添加您的
routes.rb
文件?我能想到的唯一剩下的问题是,您没有正确地放置命令(比如将其放置在泛型/:controller/:action行后面等等)。@Marcel,它是文件中的第一行(在
ActionController::Routing…
之后)。如果我理解正确,我是否应该将所有旧链接从
edit\u post\u path(post)
重命名为
edit\u news\u path(post)
?我试过了,但在删除/显示时我应该写什么?因为我得到了一个错误,比如
未定义的方法'post_path'
,但是没有任何东西包含'post'(我在for中重命名了变量,如下所示:
)你能添加
rake路由的输出吗
映射时会发生什么?参考资料:posts,:as=>'news'
,并尝试访问
/news
?@Marcel,我添加了输出@Alex,在这种情况下,我得到了
路由错误-没有路由与“{:method=>:get}
匹配“/news”,但它可以与
posts
.Hm一起工作,好的。在我看来,一切都应该正常。您是否也可以添加您的
routes.rb
文件?我能想到的唯一剩下的问题是,您没有正确地放置命令(比如将其放置在泛型/:controller/:action行后面等等)。@Marcel,它是文件中的第一行(在
ActionController::Routing…
之后)。如果我理解正确,我是否应该将所有旧链接从
edit\u post\u path(post)
重命名为
edit\u news\u path(post)
?我试过了,但在删除/显示时我应该写什么?因为我得到了一个错误,比如
未定义的方法'post\u path'
,但是没有任何东西包含'post'(我在for中重命名了变量,如下所示:
),我按照你说的做了,但我必须在
routes.rb
中添加
:singular=>:news\u instance
,并将
新新闻路径
替换为
新新闻实例路径
。还有一些问题需要解决。但是我不知道为什么这个页面上带有
productos
的例子不起作用:我确实像你说的那样,但我必须在
routes.rb
中添加
:singular=>:news\u instance
,并将
news\u path
替换为
new\u news\u instance\u path
。还有一些问题需要解决。但是我不知道为什么这个页面上带有
productos
的例子不起作用:你是