Beautiful url

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.

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

Regards.

Franco Catena.

My mistake, http://api.rubyonrails.org/classes/ActionController/Routing.html

Regards.

Franco Catena.

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:

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?

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?