views:

206

answers:

1

I am trying to make my urls prettier and still use restful resources. I understand that you can override the to_param method if you object has a name property like this:

def to_param
  self.name
end

which will give you the route /:model/:name. This is all straightforward, but I have to be capable of having the same name with multiple different languages. I haven't been able to find a blog entry on how to do this, so how can i override the to_param method to provide me a route similar to /:model/:language/:name ?

+1  A: 

You could always do:

/language/:language/model/:name

You'd do this with nested routes:

map.resources :languages do |l|
  l.resources :profiles
end

Then your route would be:

langauge_profile_url('spanish', @profile)

However...

Depending on what you're trying to do you might be better of using the built in rails i18n stuff. Is this so users can browse the site in different languages??

jonnii
ok, how do I do this?The my model is "person", so a person named "bob" who speaks "spanish" would need to be represented as /language/spanish/person/bob according to this answer. Seems reasonable, can you point me to a resource on how to do this?
ThinkBohemian
do you know how to set up routes to do something like that?
ThinkBohemian
@snowmaninthesun added more stuff.
jonnii
i actually already using i18n though i have duplicate name pairs in my database (with a different language field) I was hoping that I could get away without having to specify anything special and have my old routes "just work" in the grand scheme of things i guess it doesn't take that much extra time.
ThinkBohemian
You could always specify the language and store it in a cookie? That way you don't have to show it in the route.
jonnii