Rails 3路由如何匹配多个? [英] Rails 3 routing how to match multiple?
本文介绍了Rails 3路由如何匹配多个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何匹配多个控制器(例如一个ID)?
How do I match multiple controller for example an id?
我已经在路线上尝试过此操作
I have tried this in my routes:
match '/:id' => 'kategoris#show'
match '/:id' => 'tags#show'
推荐答案
如果要匹配http://example.com/<something>
,则不适合使用导轨控制器路由.
Rails controller routing isn't appropriate for you if you're wanting to match http://example.com/<something>
.
您可以创建一个ThingsController:
You could create a single ThingsController:
match '/:id' => 'things#show'
,然后在ThingsController中执行适当的操作.
and then do something appropriate in your ThingsController.
例如.在Sinatra(您可以将其安装为Rack中间件)中进行以下操作:
Eg. in Sinatra (which you could mount as a Rack middleware) you'd do this:
get "/:id" do :id
if(@tag = Tag.find(:id))
haml :tag
elsif(@category = Category.find(:id))
haml :category
else
pass #crucially passes on saying 'not found anything'.
end
end
无论哪种方式,您都会从RESTful Rails宣传人员那里听到痛苦的声音.
You're going to get a scream of anguish from the RESTful Rails envangelists either way.
这篇关于Rails 3路由如何匹配多个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文