Pretty URL in Rails when linking -


let's have ruby on rails blogging application post model. default able read posts http//.../post/id. i've added route

map.connect ':title', :controller => 'posts', :action => 'show' 

that accept http//.../title (titles unique) , controller query title , display page. when calling <%= link_to h(post.title), post %> in view, rails still gives me links of type post/id.

is possible rails automatically create pretty links me in case?

if willing accept: http:/.../1234-title-text can do:

def to_param   [id, title.parameterize].join("-") end 

ar::base.find ignores bit after id, "just works".

to make /title go away, try naming route:

map.post ':id', :controller => 'posts', :action => 'show', :conditions => {:id => /[0-9]+-.*/ } 

ensure route appears after map.resources :posts call.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -