Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Jon Smock's solution will work, too. I tend to prefer the following.</p> <pre><code>class Hamburger &lt;&lt; ActiveRecord::Base #this normally defaults to id def to_param name end end class SomeModelController &lt;&lt; ApplicationController def show @hamburger = Hamburger.find(params[:id]) #still default code end end #goes in some view This is the &lt;%= link_to "tastiest hamburger ever", url_for(@hamburger) %&gt;. </code></pre> <p>This is, loosely speaking, an SEO technique (beautiful URLs are also user-friendly and I suggest them to absolutely everyone even if you don't care about SEO, for example on pages behind a login). I have a more extended discussion of Rails SEO, which includes other tips like this, <a href="http://www.bingocardcreator.com/rails-seo-tips.htm" rel="nofollow noreferrer">here</a>.</p> <p><strong>Important tip:</strong> You should consider, at design-time, what you are going to do if the <code>param</code> should change. For example, in my hamburger scenario, it is entirely possible that I might rename "Sinfully Delicious Cheeseburger" to "Triple Bypass". If that changes URLs, there are some possible implications, such as breakage of customer links to my website. Accordingly, for production use I usually give these models an immutable <code>permalink</code> attribute which I initialize to be human-meaningful <em>exactly once</em>. If the object later changes, oh well, the URL stays the same. (There are other solutions -- that is just the easiest one.)</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