views:

53

answers:

2

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

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
A: 

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!

Sonia
I thought there is a smarter way to do that. I want to point all three urls pointing to the same action with the name of the category as a parameter. I concertize the question a little bit.
Fu86
Sure, the suggestion above is perfect then!
Sonia
+2  A: 

I haven't actually tested it, but try out:

match ':category' => 'products#show_category', :constraints => { :category => /this|that|andthat/ }
PreciousBodilyFluids
Great, that works perfect! Thanks für the hint with the :constraints-Option
Fu86