How about this?
In your routes file:

class ProductPathConstraint
  def matches?(request)

 
Product.dynamically_generated_list_of_available_types.include?(request.params['type'])
  end
end

MyAwesome::Application.routes.draw do
  # ...

  match ':type/:id' => "products#show", :constraints =>
ProductPathConstraint.new, :as => :products

  # ...
end

This gives you paths like this:

products_path(@product.type, @product.id)
products_path('car',123)    # => /car/123

Bayan


On Mon, Nov 29, 2010 at 2:05 PM, Ben Hoskings <[email protected]> wrote:

> You can restrict the params so the route doesn't over-match:
>
>     match '/:type/:id' => 'products#show', :type => /cars|trucks|boats/,
> :id => /\d+/
>
> Ben
>
>
> On 29 November 2010 13:57, Pat Allan <[email protected]> wrote:
>
>> I think the issue would more be the routing with the rest of the app -
>> '/:type/:id' would match  a good number of paths you wouldn't actually want
>> it to.
>>
>> I think Andy's got it spot on before - you do indeed use that route, but
>> it becomes your catch-all, at the end of your routes file.
>>
>> There's still issues with being able to generate the paths neatly, but a
>> helper method or two should take care of that. The following may even work:
>>
>>  def link_to(*args, &block)
>>    if args[1] && args[1].is_a?(Product)
>>      # build your product path here
>>    else
>>      super
>>    end
>>  end
>>
>> Could definitely be cleaner, but that's what I'd start with.
>>
>> --
>> Pat
>>
>> On 29/11/2010, at 1:51 PM, Ben Hoskings wrote:
>>
>> > How about just:
>> >
>> >     match '/:type/:id' => 'products#show'
>> >
>> > And then in ProductsController#show, use params[:id] and params[:type] ?
>> >
>> > Ben
>> >
>> > On 29 November 2010 09:44, Mark Ratjens <[email protected]> wrote:
>> > The requirement is to have urls of the form
>> >
>> > /cars/23
>> >
>> > not
>> >
>> > /products/cars-23
>> >
>> > This will, of course, be my fall-back position if I can't get exactly
>> what is wanted.
>> >
>> > Also, I'm already using friendly_id ... my id's aren't actually numeric,
>> but it doesn't change the problem. I've read though the friendly_id doco ...
>> it doesn't seem to help with adapting the url beyond id's and nested id's. I
>> have read at least one post where Norman Clark has answered saying that
>> friendly_id is not  aimed at solving routing issues.
>> >
>> >
>> > On Mon, Nov 29, 2010 at 9:35 AM, Andy Shen <[email protected]> wrote:
>> > You could consider mapping the following route to products controller
>> >
>> > /products/:type/:id
>> >
>> > or have a look into friendly_id, which is along the same concept of
>> > to_params, maybe use id like car-23, truck-37, boat-126
>> >
>> > On 28 November 2010 23:52, Mark Ratjens <[email protected]> wrote:
>> > > Suppose I have an ActiveRecord called Product. I can have different
>> kinds of
>> > > products, say "cars", "trucks" and "boats." Because of the nature of
>> the
>> > > app, there is no need to subclass product (i.e, the data and behaviour
>> is
>> > > the same) ...
>> > >
>> > > ... but for SEO reasons I want to be able to generate links in the app
>> that
>> > > include the kind of product, not just "product", for example:
>> > >
>> > > /cars/23
>> > > /trucks/37
>> > > /boats/126
>> > >
>> > > not:
>> > >
>> > > products/23 etc
>> > >
>> > > The thing I am tousling with is generating the appropriate link in the
>> app.
>> > > Is there some way I can adapt:
>> > >
>> > > link_to @product.name, @product
>> > >
>> > > to automagically generate a link with the right product kind ... or do
>> I
>> > > need to do it long-hand, i.e:
>> > >
>> > > link_to @product.name, :controller => @product.kind, :action =>
>> :show, :id
>> > > => :product
>> > >
>> > > ?
>> > >
>> > > Thanks in advance
>> > >
>> > > Mark
>> > >
>> > >
>> > > --
>> > > You received this message because you are subscribed to the Google
>> Groups
>> > > "Ruby or Rails Oceania" group.
>> > > To post to this group, send email to [email protected].
>> > > To unsubscribe from this group, send email to
>> > > [email protected]<rails-oceania%[email protected]>
>> .
>> > > For more options, visit this group at
>> > > http://groups.google.com/group/rails-oceania?hl=en.
>> > >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> Groups "Ruby or Rails Oceania" group.
>> > To post to this group, send email to [email protected].
>> > To unsubscribe from this group, send email to
>> [email protected]<rails-oceania%[email protected]>
>> .
>> > For more options, visit this group at
>> http://groups.google.com/group/rails-oceania?hl=en.
>> >
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> Groups "Ruby or Rails Oceania" group.
>> > To post to this group, send email to [email protected].
>> > To unsubscribe from this group, send email to
>> [email protected]<rails-oceania%[email protected]>
>> .
>> > For more options, visit this group at
>> http://groups.google.com/group/rails-oceania?hl=en.
>> >
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> Groups "Ruby or Rails Oceania" group.
>> > To post to this group, send email to [email protected].
>> > To unsubscribe from this group, send email to
>> [email protected]<rails-oceania%[email protected]>
>> .
>> > For more options, visit this group at
>> http://groups.google.com/group/rails-oceania?hl=en.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby or Rails Oceania" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<rails-oceania%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/rails-oceania?hl=en.
>>
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Ruby or Rails Oceania" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<rails-oceania%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/rails-oceania?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
or Rails Oceania" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rails-oceania?hl=en.

Reply via email to