Add the following to the bottom of your config/routes.rb
map.connect '*url', :controller => 'acts', :action => 'show_page'
Then define the following in app/controllers/acts_controller.rb
def show_page
url = params[:url]
if Array === url
url = url.join('/')
else
url = url.to_s
end
# you now have the path in url
@act = Acts.find_by_name(url)
if @act
render :action => :show
else
redirect_to some_error_page, :status => 404
end
end
A few gotchas with the above approach.
- The route is a catch all. You will be trapping everything that doesn't match a route above it. So make sure it's last and make sure you are ready to handle 404s and the like.
- The
:url
param is an array or a string depending on the route coming in. For example /blacktie/night
will be an array with a value of ['blacktie', 'night']
. That's why I joined them with in the beginning of show_page. So your find_by_name
function could be really smart and allow for nested acts and the such.
Hope this helps.
OR...
Add this to routes (at the bottom):
map.connect ':name', :controller => "acts", :action => "show_page",
:requirements => {:name => /[\w|-]*/}
This tells rails to send anything matching the requirements to your handler. So your show_page would be like the following:
def show_page
@act = Acts.find_by_name(params[:name])
if @act
render :action => :show
else
redirect_to some_error_page, :status => 404
end
end
This gets rid of the some of the gotchas but gives you less options for nesting and the like.