Forum: Ruby on Rails route a name to an id?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Andrew K. (Guest)
on 2005-12-21 20:51
i have a database setup with an id and a field called uniquename

say for example my db looks like this:

<table border="1" cellpadding="1" cellspacing="1">
  <tr>
    <td>id</td>
    <td>uniquename</td>
  </tr>
  <tr>
    <td>1</td>
    <td>pizza</td>
  </tr>
  <tr>
    <td>2</td>
    <td>salad</td>
  </tr>
</table>

at the moment the routing works as follows:

<b>domain.com/recipe/show/2</b>

instead i'd like it to route from:

<b>domain.com/recipe/show/salad</b>

any ideas?
Jake J. (Guest)
on 2005-12-22 02:02
Andrew Kendall wrote:
> i have a database setup with an id and a field called uniquename
>
> say for example my db looks like this:
>
> <table border="1" cellpadding="1" cellspacing="1">
>   <tr>
>     <td>id</td>
>     <td>uniquename</td>
>   </tr>
>   <tr>
>     <td>1</td>
>     <td>pizza</td>
>   </tr>
>   <tr>
>     <td>2</td>
>     <td>salad</td>
>   </tr>
> </table>
>
> at the moment the routing works as follows:
>
> <b>domain.com/recipe/show/2</b>
>
> instead i'd like it to route from:
>
> <b>domain.com/recipe/show/salad</b>
>
> any ideas?


You can do this as-is.  Just use the params[:id] differently.  i.e.,
search for it in the database:

   item = Items.find_by_uniquename(params[:id])

I'm a newbie answering a question, so take this with a grain of salt.

   Jake
This topic is locked and can not be replied to.