class Content < ActiveRecord::Base
has_friendly_id :title, :use_slug => true
end
How I can make a link like /about-us instead of /contents/about-us ?
Should I modify the content_url method , or is there a better approach ?
class Content < ActiveRecord::Base
has_friendly_id :title, :use_slug => true
end
How I can make a link like /about-us instead of /contents/about-us ?
Should I modify the content_url method , or is there a better approach ?
OK, re-doing this answer. I have looked into this a bit more including the has_friendly_id plugin, seems nice, but have not used that before. I kinda rolled my own methods for making friendly urls in the past.
I think I now understand what you asking for... each title in your Contents table, you want a friendly url, and you want that url to start at / (root)
, not under /content
. I don't see an immediate way to do this with friendly_id, but that is OK, routes.rb does this quite easily.
Modify routes.rb to make content
your root
:
map.root :controller => 'content'
I think multiple map.root calls can be made, it just depends on order if there are collisions. If not, it's just an alias of sorts for
map.connect '', :controller => 'content'
Good luck!
I have just done this for a site I'm working on.
Its actually a matter of specifying the correct routes.
As your model is called Content, I presume you have already mapped Content as a resource like this (in your routes.rb):
map.resources :content
This will handle urls like:
http://example.com/content/my-special-content-page
To handle urls like:
http://example.com/my-special-content-page
you simply need to map the routes like this:
map.content '/:id', :controller => 'content', :action => 'show'
Note:
Routes work from top to bottom, so you'll need to put this below most things. Especially the:
map.root :controller => "welcome"
If you put your new route above this, you'll end up with an error because it will attempt call the Controller.show action with an :id of nil.
You'll also need to ensure wherever you are generating urls in your views you'll need to use this new route like this:
= link_to "My Special Page", content_path(@content)