# A more graceful friendly url method?

**URL:** <https://www.ruby-forum.com/t/a-more-graceful-friendly-url-method/182385>\
**Category:** Rails\
**Created:** [January 27, 2010, 11:41am UTC](https://www.ruby-forum.com/t/a-more-graceful-friendly-url-method/182385 "2010-01-27T11:41:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [January 27, 2010, 11:41am UTC](https://www.ruby-forum.com/t/a-more-graceful-friendly-url-method/182385/1 "2010-01-27T11:41:27Z")

</div>

Hey,

I’m almost done a project and only now am I implementing friendly  
URLs. I’m using the method from “Simply Rails 2” by Patrick Lenz.

This involves the following addition to the model:

def to\_param  
“#{id}-#{name}”.gsub(/\W/, ‘-’).downcase  
end

So every time a url is generated it creates a friendly URL.

I’m now having trouble with my controllers though.  
if i use @item = Item.find(params[:id]) # Everything works fine.

I have in many spots magick find methods like  
@item = Item.find\_by\_id(params[:id])  
or  
@item = Item.find\_by\_associated\_model\_id(params[:id])

Which will actually attempt to find the entire friendly URL string as  
an ID in the database.

I can fix the problem with  
@item = Item.find\_by\_associated\_model\_id(params[:id].to\_i)  
As this will strip the appended string. But I feel theres probably  
something I’m missing / a more graceful way.

Thanks,  
brianp

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [January 27, 2010, 6:28pm UTC](https://www.ruby-forum.com/t/a-more-graceful-friendly-url-method/182385/2 "2010-01-27T18:28:19Z")

</div>

On Jan 27, 2010, at 2:39 AM, brianp wrote:

> Which will actually attempt to find the entire friendly URL string as  
> an ID in the database.
> 
> I can fix the problem with  
> @item = Item.find\_by\_associated\_model\_id(params[:id].to\_i)  
> As this will strip the appended string. But I feel theres probably  
> something I’m missing / a more graceful way.

I don’t think there is… find() converts it’s argument to an  
integer. The other’s do not. Might be an interesting patch since you  
should be able to determine what the underlying data type is and then  
cast it.

And while you’re to\_param is fine, you might check out

> **[GitHub - rsl/stringex: Some \[hopefully\] useful extensions to Ruby’s String...](https://github.com/rsl/stringex)**
>
> Some \[hopefully\] useful extensions to Ruby’s String class. It is made up of three libraries: ActsAsUrl \[permalink solution with better character translation\], Unidecoder \[Unicode to Ascii translite...

. It does something similar, but is has some more smarts under the  
hood (expanding symbols, etc.). No affiliation, just like it.

```
# A simple prelude
"simple English".to_url => "simple-english"
"it's nothing at all".to_url => "its-nothing-at-all"
"rock & roll".to_url => "rock-and-roll"

# Let's show off
"$12 worth of Ruby power".to_url => "12-dollars-worth-of-ruby-power"
"10% off if you act now".to_url => "10-percent-off-if-you-act-now"

# You don't even wanna trust Iconv for this next part
"kick it en FranÃ§ais".to_url => "kick-it-en-francais"
"rock it EspaÃ±ol style".to_url => "rock-it-espanol-style"
"tell your readers ä½ å¥½".to_url => "tell-your-readers-ni-hao"

```

---

<div class="post-metadata">

**Author:** ![brianp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@brianp](https://www.ruby-forum.com/u/brianp)\
**Post date:** [January 28, 2010, 4:35am UTC](https://www.ruby-forum.com/t/a-more-graceful-friendly-url-method/182385/3 "2010-01-28T04:35:57Z")

</div>

Hey thanks for the reply, [GitHub - rsl/stringex: Some [hopefully] useful extensions to Ruby’s String class. It is made up of three libraries: ActsAsUrl [permalink solution with better character translation], Unidecoder [Unicode to Ascii transliteration], and StringExtensions [miscellaneous helper methods for the String class].](http://github.com/rsl/stringex) does look  
very handy I’ll keep it in mind in the future.

Just out of curiosity what did you mean by “Might be an interesting  
patch since you  
should be able to determine what the underlying data type is and  
then  
cast it.”

as in maybe I should write a helper that checks the data type and  
passed it to the find ? or returns the desired results… hmmm I  
suppose i do have some options.

Thanks for the reply!
