我已经阅读了有关 Rails 3 中路由如何工作的资源,但遇到了一些困难。
在我的应用程序中有博客,并且我的routes.rb包含:
resources :blogs
root :to => "home#index"
包含“博客”的URL工作正常。
不过,我想做的是在网址中包含“博客”。具体来说,/blog/:id(用于显示)和 /blog(用于索引)。
如果我添加以下行:
match 'blog' => 'blogs#index'
然后 /blog 确实显示索引,但是当操作 URL 从 /blog/:id 更改为 /blog.:id 时,它会破坏我的博客编辑表单
关于如何使用博客而不是博客的任何想法,以及还允许博客索引位于 /blog 上吗?我已经尝试了很多东西(例如资源:博客,以及资源:博客,:as => '博客'),但没有取得任何进展。假设有一种方法可以做到这一点,而无需手动定义显示、编辑、销毁和索引的每个路由。
I've read around the resources on how routing in Rails 3 works, but am running into some difficulties.
In my app there are Blogs and my routes.rb contains:
resources :blogs
root :to => "home#index"
URLs containing 'blogs' are work fine.
However what I'd like to do is have 'blog' in the URLs. Specifically, /blog/:id (for show) and /blog (for index).
If I add the line:
match 'blog' => 'blogs#index'
Then /blog does show the index, however it breaks my blog edit form as the action URL changes from /blog/:id to /blog.:id
Any ideas on how to use blog instead of blogs, and also allow the blog index to be on /blog? I've tried quite a few things (like resource :blog, and also resources :blogs, :as => 'blog') and not getting anywhere. Assuming there's a way to do this without manually defining each route for show, edit, destroy and index.
发布评论
评论(2)
您需要使用 :path 选项
You need use the :path option
删除
match
行,并将resouces
行更改为:检查:http://edgeguides.rubyonrails.org/routing.html#translated-paths
Remove your
match
line, and change yourresouces
line to:Check: http://edgeguides.rubyonrails.org/routing.html#translated-paths