我正在尝试创建一个简单的博客,其中 posts Controller 路由到根 URL,而不是 localhost/posts/:id。
我将以下内容添加到我的路线文件中...
match '/:id', :to => 'posts#show', :as => 'post'
效果很好。它从此打破了我的搜索路线。
match 'search/:q', :to => 'posts#query', :as => 'search'
看来 Rails 正在尝试查找具有我的搜索查询 ID 的帖子。有没有办法执行类似以下操作,让路由文件假定根 url 之后的所有内容都是帖子 id,除了以“搜索”开头的任何内容?
match '/:id', :to => 'posts#show', :as => 'post', :except => 'search'
最佳答案
Rails 从上到下解析您的路线并在第一个匹配处停止。我会将您的 match '/:id'...
行放在所有其他路线下方。
关于ruby-on-rails - rails 路由:except question,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6245759/