Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Quick Solution</h2> <p>Have a catch-all route at the bottom of routes.rb. Implement any alias lookup logic you want in the action that route routes you to. </p> <p>In my implementation, I have a table which maps defined URLs to a controller, action, and parameter hash. I just pluck them out of the database, then call the appropriate action and then try to render the default template for the action. If the action already rendered something, that throws a DoubleRenderError, which I catch and ignore. </p> <p>You can extend this technique to be as complicated as you want, although as it gets more complicated it makes more sense to implement it by tweaking either your routes or the Rails default routing logic rather than by essentially reimplementing all the routing logic yourself.</p> <p>If you don't find an alias, you can throw the 404 or 500 error as you deem appropriate.</p> <h2><strong>Stuff to keep in mind:</strong></h2> <p><strong>Caching:</strong> Not knowing your URLs a priori can make page caching an absolute bear. Remember, it caches based on the URI supplied, NOT on the <code>url_for (:action_you_actually_executed</code>). This means that if you alias </p> <pre><code>/foo_action/bar_method </code></pre> <p>to</p> <pre><code>/some-wonderful-alias </code></pre> <p>you'll get some-wonderful-alias.html living in your cache directory. And when you try to sweep foo's bar, you won't sweep that file unless you specify it explicitly.</p> <p><strong>Fault Tolerance:</strong> Check to make sure someone doesn't accidentally alias over an existing route. You can do this trivially by forcing all aliases into a "directory" which is known to not otherwise be routable (in which case, the alias being textually unique is enough to make sure they never collide), but that isn't a maximally desirable solution for a few of the applications I can think of of this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload