【问题标题】:Changing the route from posts/article to blog/article in rails在 rails 中将路径从帖子/文章更改为博客/文章
【发布时间】:2015-08-01 17:40:31
【问题描述】:

我在 Rails 中遇到了路线问题。我一直在尝试获得类似的路径: home/blog/title-article 但我得到的关闭是: home/blog/posts/title-article

我已经尝试过使用命名空间、范围和 get 'blog' => 'posts#blablabla' 的方法,但每次更改路径时都会出现 UrlGenerationError 或 NameError 等错误。我已经阅读了这个论坛的官方指南和一些答案,但我越来越困惑了嘿嘿

在我最后一次尝试中,我生成了一个控制器博客和一个脚手架帖子。 rake routes 的输出为: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 谢谢!

【问题讨论】:

  • 问题是你的范围。你告诉 Rails 你想要像/blog/post/xxx 这样的路线。如果您想要 blog 的 RESTful 路由,请使用 resources :blog

标签: ruby-on-rails url routes namespaces scope


【解决方案1】:

不确定您的期望,但请尝试:

resources :posts, path: 'blog'

虽然感觉很奇怪。

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

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

Doc lives here

【讨论】:

  • 我认为他的意思是 /home 是基本 URL,因为没有提及 home 资源。
  • 是的!它有效......我想这不是我最后一次尝试 hehehe.resources :posts, path: 'blog'
猜你喜欢
  • 2019-01-02
  • 2015-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多