title: Route Controller#show method like how Controller#index would in Rails
body: <p>Hi guys I am new to rails. Sorry if I can't define this question properly.</p>
  
  <p>What I wanted is for:</p>
  
  <pre><code>domain.com/posts/1-sample-post
  </code></pre>
  
  <p>to be routed like this:</p>
  
  <pre><code>domain.com/1-sample-post
  </code></pre>
  
  <p>How do I achieve this in rails routes? I've tried searching for this for almost 3 hours. This is very easy in PHP frameworks. I thought this is easy in Rails too.</p>
  
  <p>I forgot to mention I have High_voltage gem installed in my app for my static pages.</p>
  
  <p>Did this:</p>
  
  <pre><code>#routes.rb
  resources :posts
  get '/:id' =&gt; 'posts#show'
  </code></pre>
  
  <p>Now my High_voltage pages could not be rendered.</p>
  
  <p>Update Solution:</p>
  
  <p>So here is what we did in the routes:</p>
  
  <pre><code>Rails.application.routes.draw do
    resources :authors
    constraints(lambda { |req| Author.exists?(slug: req.params["id"]) }) do
       get '/:id' =&gt; 'authors#show'
    end
  
    devise_for :users
  
    resources :posts
    constraints(lambda { |req| Post.exists?(slug: req.params["id"]) }) do
      get '/:id' =&gt; 'posts#show'
    end
  end
  </code></pre>
  
  <p>Note that it is important to only use an exists? query here as it is very fast than other methods, so it won't eat that much loading time to render a record.</p>
  
  <p>Special thanks to the guys below who helped a lot. Nathanvda, rwold, and Tai.</p>