Forum: Ruby on Rails Beautiful url

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.
Mr. Bless (Guest)
on 2009-05-20 10:42
(Received via mailing list)
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

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,
I still need the url as shown previously.
Franco C. (Guest)
on 2009-05-20 15:36
(Received via mailing list)
You can do something like
http://localhost:3000/movie/detail/1/movie_name
(with a slash) see more in
http://api.rubyonrails.org/classes/ActionControlle...

Regards.

Franco C..
Franco C. (Guest)
on 2009-05-20 15:38
(Received via mailing list)
Robert W. (Guest)
on 2009-05-20 17:20
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

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:
http://jroller.com/obie/entry/seo_optimization_of_urls_in

> How ever if the someone manually type 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. 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?
Nirvana (Guest)
on 2009-05-20 18:45
(Received via mailing list)
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>
Nirvana (Guest)
on 2009-05-21 09:00
(Received via mailing list)
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>
This topic is locked and can not be replied to.