【发布时间】: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。
我真的不知道如何解决这个问题。
谢谢! :)
【问题讨论】:
-
你应该在这里阅读更多关于路由的信息 - guides.rubyonrails.org/routing.html
-
另外,您必须在模型中定义关系
-
在此处阅读更多信息 - stackoverflow.com/questions/11888587/…
标签: ruby-on-rails rest