Advanced Routing with Rails3
I want to use regular expressions inside my routes. I have an Products controller, but I want a different URL structure to access the开发者_运维知识库 products
- http://host/this/
- http://host/that/
- http://host/andthat/
These URLs should call a action in my controller (Products:show_category(:category)
)
Is something like this possible?
match "(this|that|andthat)" => "products#show_category", :category => $1
the action should look like this
def show_category
puts params[:category] # <-- "this" if http://host/this/ is called
# ...
end
I haven't actually tested it, but try out:
match ':category' => 'products#show_category', :constraints => { :category => /this|that|andthat/ }
I'm not too sure if this answers your question, but you could add a collection to routes.rb:
resources :products do
collection do
get :category1
get :category2
get :category3
end
end
If you then run rake routes
, you'll see that you have urls like /products/category1
and products/category2
. Category1, 2 and 3 can be defined in your controller as usual:
def category1
#custom code here
end
def category2
#custom code here
end
def category3
#custom code here
end
As I said, I'm not too sure if that's what you're looking to do, but hope that helps a bit!
精彩评论