views:

182

answers:

2
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 ?

A: 

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!

dpb
I mean I have a model with the title "about us", I dont't want to modify the config/routes file everytime a new model is added to the database
astropanic
I udpated my answer a bit more. If you need more clarification, let me know. It looks like you are getting a bit confused on the purpose of routes since you keep mentioning a model. The "paths", or "links" actually map to a controller and an action. The model is then accessed by the named action method in your controller. I don't know of a way to route directly to a model. If there is a way, it certainly is not the way rails is supposed to work.
dpb
I know how rails routing work and know the action controller mechanizm, but I want such links like on http://ministerstwogadzetow.com for example, look at the two links http://www.ministerstwogadzetow.com/wyciagarka-do-slodyczy.html - this points to the products controller show action and this one http://www.ministerstwogadzetow.com/zegary-i-zegarki.html points to category controller show action (php engine), as You can see, there is no /categories/something or /products/something and this is what I want
astropanic
That's not what he's looking for, @dpb. The content isn't stored in app/views/ - it's stored in the database, with the title "About Us." Modifying `routes.rb` is most definitely part of the job here, but the OP is looking for a dynamic, database-driven solution. When he says "model," he means "object."
Matchu
Thanks @matchu. Yeah, I'm unsure how to do that, then...
dpb
+1  A: 

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)
Globalkeith
Not exactly what I want, but a good solution.What should I do if I have Two or more models, not only Content but maybe Product and Photo too ?
astropanic