Rails“/ blog”路径未初始化的常量博客

时间:2020-12-20 11:00:40

I am trying to setup a rails blog at the "website.com/blog" url

我正在尝试在“website.com/blog”网址上设置rails博客

I already have my models and controller setup to work to where going to

我已经有我的模型和控制器设置工作到哪里去

website.com/posts

Gives me all my posts and going to

给我所有的帖子,然后去

website.com/posts/1/

Shows me that post, etc, etc. What I want to happen is that when I go to

向我展示帖子等等。我想要发生的是当我去的时候

website.com/blog/

I should see the posts index (and the original URL should no longer work). Similarly I want to go to

我应该看到帖子索引(并且原始URL应该不再起作用)。同样我想去

website.com/blog/posts/1/

To see that post and so on and so forth.

要查看该帖子等等。

Right now this is my routes file:

现在这是我的路线文件:

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

When I go to "/blog/" I get a Routing Error saying "uninitialized constant Blog". Do I need to create a blog model and controller and migrate to complete this? I'd rather not since it's really just running the posts requests from that new URL. Am I going about this the wrong way?

当我去“/ blog /”时,我得到一个路由错误,说“未初始化的常量博客”。我是否需要创建博客模型和控制器并迁移才能完成此操作?我宁愿不要,因为它真的只是运行来自新URL的帖子请求。我是以错误的方式来做这件事的吗?

I ended up finding the answer to my own question here: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

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

Using this seems to work just fine:

使用它似乎工作得很好:

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

1 个解决方案

#1


The answer ended up being found here: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

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

As usual the solution was incredibly simple and made me feel like an idiot for not knowing what to do immediately:

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

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

#1


The answer ended up being found here: http://guides.rubyonrails.org/routing.html#controller-namespaces-and-routing

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

As usual the solution was incredibly simple and made me feel like an idiot for not knowing what to do immediately:

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

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