1-st level routes for multiple resources in Rails

Posted by Leonid Shevtsov on Stack Overflow See other posts from Stack Overflow or by Leonid Shevtsov
Published on 2010-03-24T11:01:10Z Indexed on 2010/03/24 11:03 UTC
Read the original article Hit count: 217

Filed under:
|
|

I have a simple SEO task. There's a City model and a Brand model, and I have to create 1st-level URLs for both (e.g. site.com/honda and site.com/boston).

What's the preferred routing/controller combination to do this in Rails? I can only think of

map.connect '/:id', :controller => 'catchall', :action => 'index'

class CatchallController < ApplicationController
  def index
    if City.exists?(:slug => params[:id])
      @city = City.find_by_slug!(params[:id])
      render 'cities/show'
    else
      @brand = Brand.find_by_slug!(params[:id])
      render 'brands/show'
    end
  end
end

but it seems to be very un-Rails to put such logic into the controller.

(Obviously I need to make sure that the slugs don't overlap in the models, that's done).

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about routing