It looks like you are saying here that if you cannot find the page belonging to the hash, redirect to "/"
There's something really smelly about doing ActiveRecord searches on your routes.
If you need to redirect to a specific controller depending on the type of sharing, I would put it as a separate controller with redirection:
match "/:hash" => 'SharableController#redirect':constraints => { :hash => /[a-zA-Z0-9]{7}/ }
and then process the record search and redirect to the correct controller action from there:
class SharableController < ApplicationController def redirect @sharable = Sharable.find_by_sharable_type(params[:hash]) redirect_to controller: @sharable.sharable_type, action: 'show', id: @sharable.id end end
OR ... depending on how similar the show actions are:
class SharableController < ApplicationController def redirect @sharable = Sharable.find_by_sharable_type(params[:hash]) render template: "#{@sharable.sharable_type.pluralize}/show" end end
If you deal only with GET requests, it is better to change the match to get the way:
get "/:hash" => 'SharableController#redirect', :constraints => { :hash => /[a-zA-Z0-9]{7}/ }
source share