Rails "/blog" 路径未初始化常量博客

Rails "/blog" path uninitialized constant Blog

我正在尝试在 "website.com/blog" url

上建立一个 rails 博客

我已经设置了模型和控制器,可以在要去的地方工作

website.com/posts

给我所有的 post 然后去

website.com/posts/1/

告诉我post,等等。我想要发生的是,当我去

website.com/blog/

我应该看到 posts 索引(原来的 URL 应该不再起作用了)。同样我想去

website.com/blog/posts/1/

看到那个post等等等等。

现在这是我的路线文件:

Rails.application.routes.draw do
  namespace :blog do 
    resources :posts do 
      resources :comments
    end
  end 
  get "/blog", to: "posts#index"
end

当我转到“/blog/”时,我收到一个路由错误 "uninitialized constant Blog"。我是否需要创建博客模型和控制器并进行迁移才能完成此操作?我宁愿不这样做,因为它实际上只是 运行 来自那个新 URL 的 post 的请求。我会以错误的方式解决这个问题吗?

我最终在这里找到了我自己问题的答案:http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

使用这个似乎工作得很好:

scope '/blog' do
    resources :posts do 
        resources :comments
    end
end 
get "/blog", to: "posts#index"

答案最终在这里找到:http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

像往常一样,解决方案非常简单,让我觉得自己像个白痴,不知道该怎么做:

scope '/blog' do
    resources :posts do 
        resources :comments
    end
end 
get "/blog", to: "posts#index"