【问题标题】:Rails Multiple-User Blog RoutingRails 多用户博客路由
【发布时间】:2014-09-25 14:51:51
【问题描述】:

我需要一些关于 RESTful 路由的帮助。基本上,我正在尝试重新创建 http://medium.com 以帮助提高我的 Rails 技能。

我已经通过 Devise 完成了注册。

我卡在实际的博客路由上。我希望路线是这样的:domain.com/username/post-title。我不确定是否有可能使这种应用程序遵循restful模式。

我该怎么做?

我正在考虑做这样的事情:

scope module: :username do
  resources :posts
end

但是当我搜索路线时,我会得到一些奇怪的东西:

 posts GET    /posts(.:format)                  username/posts#index
                     POST   /posts(.:format)                  username/posts#create
  new_post GET    /posts/new(.:format)              username/posts#new
           edit_post GET    /posts/:id/edit(.:format)         username/posts#edit
                post GET    /posts/:id(.:format)              username/posts#show
                     PATCH  /posts/:id(.:format)              username/posts#update
                     PUT    /posts/:id(.:format)              username/posts#update
                     DELETE /posts/:id(.:format)              username/posts#destroy
                root GET    /                                 home#index

这些路由的 url 仍然是 /posts 并且由于某种原因不是 :username/posts。

我真的不知道如何解决这个问题。

谢谢! :)

【问题讨论】:

标签: ruby-on-rails rest


【解决方案1】:

那是因为您使用的是“范围”。如果您想在 url 中使用模块名称,请尝试使用“命名空间”

namespace :username do
  resources :posts
end

【讨论】:

    猜你喜欢
    • 2016-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 1970-01-01
    相关资源
    最近更新 更多