# Beautiful url

**URL:** <https://www.ruby-forum.com/t/beautiful-url/168380>\
**Category:** Rails\
**Created:** [May 20, 2009, 8:42am UTC](https://www.ruby-forum.com/t/beautiful-url/168380 "2009-05-20T08:42:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 20, 2009, 8:42am UTC](https://www.ruby-forum.com/t/beautiful-url/168380/1 "2009-05-20T08:42:37Z")

</div>

How to have a beautiful url like hulu. For example I like to have a  
url like this to show the movie detail with an id 1 as  
[http://localhost:3000/movie/detail/1-movie\_name](http://localhost:3000/movie/detail/1-movie_name)

If via a link, i can do this by writing the to\_param method in movie  
model like this  
def to\_param  
“#{id}-#{name}”  
end

and a like like this  
\<%=link\_to  
‘Detail’, :controller=\>‘movie’,:action=\>‘detail’,:id=\>@movie %\>

How ever if the someone manually type [http://localhost:3000/movie/1](http://localhost:3000/movie/1),  
I still need the url as shown previously.

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 20, 2009, 1:36pm UTC](https://www.ruby-forum.com/t/beautiful-url/168380/2 "2009-05-20T13:36:57Z")

</div>

You can do something like  
[http://localhost:3000/movie/detail/1/movie\_name](http://localhost:3000/movie/detail/1/movie_name)  
(with a slash) see more in  
[http://api.rubyonrails.org/classes/ActionController/Routing/Helpers.html](http://api.rubyonrails.org/classes/ActionController/Routing/Helpers.html)

Regards.

Franco C…

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 20, 2009, 1:38pm UTC](https://www.ruby-forum.com/t/beautiful-url/168380/3 "2009-05-20T13:38:07Z")

</div>

My mistake,  
[http://api.rubyonrails.org/classes/ActionController/Routing.html](http://api.rubyonrails.org/classes/ActionController/Routing.html)

Regards.

Franco C…

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 20, 2009, 4:45pm UTC](https://www.ruby-forum.com/t/beautiful-url/168380/4 "2009-05-20T16:45:00Z")

</div>

Thanks for the suggestion. I do it something like this have a regular  
expression check on the request\_uri and if it does not have the  
desired format just redirect to the same method with :id=\>@movie

On May 20, 6:20 pm, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 20, 2009, 3:20pm UTC](https://www.ruby-forum.com/t/beautiful-url/168380/5 "2009-05-20T15:20:02Z")

</div>

Mr. Bless wrote:

> How to have a beautiful url like hulu. For example I like to have a  
> url like this to show the movie detail with an id 1 as  
> [http://localhost:3000/movie/detail/1-movie\_name](http://localhost:3000/movie/detail/1-movie_name)

First of all, beauty is in the eye of the beholder. Assuming that  
/movie/1 and /movie/detail/1 are representing the exact same resource  
then, to me, /movie/1-movie\_name would be “more beautiful” than  
/movie/detail/1-movie\_name because it removes the redundancy of  
/movie/detail. Now if the two are different resources (or  
representations of a resource) then that’s different.

> If via a link, i can do this by writing the to\_param method in movie  
> model like this  
> def to\_param  
> “#{id}-#{name}”  
> end

You probably better take care of URL escaping #{name}.

> and a like like this  
> \<%=link\_to  
> ‘Detail’, :controller=\>‘movie’,:action=\>‘detail’,:id=\>@movie %\>

This article might help:

> **[Best Online Casino in Australia | Top Licensed Casinos for Gamblers](https://toponlinecasinoaustralia.com)**
>
> Best Online Casino in Australia with licensed games only. Play blackjack, roulette, poker and other top-notch casino games. Enjoy gambling 24/7.

> How ever if the someone manually type [http://localhost:3000/movie/1](http://localhost:3000/movie/1),  
> I still need the url as shown previously.

Again, I reiterate; Is /movie/1 the same representation of the same  
resource as /movie/detail/1? Not that it matters, you can have as many  
different URIs representing the same representation as you want. I’m  
just attempting to clarify.

I can think of two ways to change /movie/1 to show as  
/movie/detail/1-movie\_name if that’s what the user enters into the  
address field. 1. Have the movies\_controller’s “show” action redirect to  
the other URI, or 2. Replace the address field value using JavaScript.

How often do you expect an end user to type something like  
[http://example.com/movies/354](http://example.com/movies/354). How would they have any idea what id goes  
with what movie. So if they were to type in the URI directly, what  
difference would it make? It would happen so rarely I doubt it would  
have an adverse affect on your search engine results. Basically, people  
aren’t going to be doing that, so why engineer a solution for a problem  
that doesn’t really exist?

---

<div class="post-metadata">

**Author:** ![MrSBless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/3bc359/32.png) [@MrSBless](https://www.ruby-forum.com/u/MrSBless)\
**Post date:** [May 21, 2009, 7:00am UTC](https://www.ruby-forum.com/t/beautiful-url/168380/6 "2009-05-21T07:00:10Z")

</div>

Thanks for the suggestions. I do it something like this, a regular  
expression check on the request\_uri and if it does not have the  
desired format just redirect to the same method with :id=\>@movie. Is  
there more cleaner approach than this?

On May 20, 6:20 pm, Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
