在 rails 中将路线从 posts/article 更改为 blog/article

Changing the route from posts/article to blog/article in rails

我在 Rails 的路线上遇到了麻烦。我一直在尝试获得如下路径:home/blog/title-article 但我得到的结果是:home/blog/posts/title-article

我尝试过使用命名空间、作用域,并使用 get 'blog' => 'posts#blablabla' 逐一尝试,但每次更改路径时都会出现 UrlGenerationError 或 NameError 等错误。看了官方的攻略和本论坛的一些回答,越来越糊涂了嘿嘿

在我最后一次尝试中,我生成了一个控制器博客和一个脚手架 Post。 rake 路由的输出是:http://i.stack.imgur.com/gdfPc.png

routes.rb

Rails.application.routes.draw do

  scope :blog do
    resources :posts
  end

  get 'blog' => 'blog#index'

  root 'pages#index'

...

谢谢!

现在我的路线是这样的:http://i.stack.imgur.com/cKsFG.png 谢谢!

不确定是否如您所愿,但请尝试:

resources :posts, path: 'blog'

虽然感觉很奇怪。

顺便说一句,我猜你的错误是由于 url 助手造成的。

使用rake routes检查现有路由和相关的url助手。

Doc lives here