ruby - Overwrite generated restful url helpers in Rails -
lets have page resource, , particular instance has id = 5 , permalink = foobar.
with resources :pages
can use <%= link_to @page.title, @page %>
outputs url "/pages/5".
how make output "/pages/foobar" instead? likewise edit url... how make edit_page_path(@page)
output "/pages/foobar/edit"?
update
answers far have said override to_param
in page.rb great start. +1 each. if want <%=link_to @page.title, @page%>
output "/:permalink" rather "/pages/:permalink"?? i'll accept answer comes that.
you can override to_param
method in model tell rails use instead of primary key routing.
for example
class page def to_param "#{self.id}-#{self.title.parameterize}" end end
the parameterize call makes title url friendly, might notice use of self.id, recommended in case have duplicate title.
Comments
Post a Comment