>
> I defined the following in routes.rb to use ':title' instead of ':id'.
>
> map.movie '/
> movies/:title', :controller=>'movies', :action=>'show',
> :conditions=>{:method=>:get}
>
> This seems to work fine, but 'movie_path()' still uses ':id' instead
> of ':title'.
>
> movie = Movie.get(parms[:id])
> p movie_path(movie) #=> "/movies/1" (expected is "/movies/Avatar")
>
> How to change 'movie_path()' to use ':title'?
>
The reason this happens is that movie_path converts an object to a parameter
using the to_param method of the object, rather than knowing what parameter
the route wants.
So, there are two solutions:
1)
class Movie
def to_param
name
end
end
2)
movie_path(movie.title)
Depending on how many other paths rely on you passing a movie around, that
will affect your decision. A little known third option is this:
class Movie
def to_param
"#{id}-#{name}"
end
end
That way you can just use the param :id in your path (and 1-Avatar will be
converted to the integer 1 when searched using the :id field) but the title
of the movie will be in the URLs for SEO purposes.
Cheers,
Andy
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.