Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>We've actually done something like this. It's not pretty, but this solution worked for us:</p> <hr> <p><strong>Slugs</strong></p> <p>You're basically alluding to a type of your routes called <a href="https://stackoverflow.com/questions/15375222/how-to-create-app-wide-slug-routing-for-rails-app">Slugs</a>. This is where you use a name instead of an ID, allowing you to make a user-friendly route (such as <code>/delivery/today</code>). The problem is that in order to create these routes, you have to define them individually in the routes file</p> <p>There are two Gems you can use to handle your slugged routes -- <a href="https://github.com/norman/friendly_id" rel="nofollow noreferrer">FriendlyID</a> &amp; <a href="https://github.com/RISCfuture/slugalicious" rel="nofollow noreferrer">Slugalicious</a>. Both of these allow you to create slugged routes, but FriendlyID basically just changes the ID, whilst Slugalicious is a totally independent system</p> <p>We used Slugalicious for the code below, however, you'll probably want FriendlyID (there's a <a href="http://railscasts.com/episodes/314-pretty-urls-with-friendlyid" rel="nofollow noreferrer">RailsCast for it here</a>):</p> <p><img src="https://i.stack.imgur.com/ynMuz.png" alt="enter image description here"></p> <hr> <p><strong>Routing</strong></p> <p>The problem you have is that routes are outside the scope of the RESTful controller interface, which means you'll have to call all the routes exclusive of your <code>resources</code> references in the routes.rb file</p> <p>If you use Slugalicious, it has its own <code>Slugs</code> database, which means we can use it to create the routes on the fly, like this:</p> <pre><code> #Slugs begin Slug.all.each do |s| begin get "#{s.slug}" =&gt; "#{s.sluggable_type.downcase.pluralize}#show", :id =&gt; s.slug rescue end end rescue end </code></pre> <p>This is live code, and outputs all the slugs in the routes file dynamically. The way we managed to get this to update programmatically was to use an Observer Class like this:</p> <pre><code>class SlugObserver &lt; ActiveRecord::Observer def after_save(slug) Rails.application.reload_routes! end def after_destroy(slug) Rails.application.reload_routes! end end </code></pre> <p>I appreciate you may have your answer already, but as you're a beginner, I felt I could help out by explaining the slug stuff for you</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