#46 Catch-all Route

Sometimes you need to add complex/dynamic routes. This is often impossible to do in routes.rb, but do not worry. It can be accomplished with a catch-all route. See how in this episode.
# routes.rb
map.connect '*path', :controller => 'redirect', :action => 'index'

# redirect_controller.rb
def index
  product = Product.find(:first, :conditions => ["name LIKE ?", "#{params[:path].first}%"])
  redirect_to product_path(product)
end

你可能感兴趣的:(catch)