Oleg Pasko
Oleg Pasko

Reputation: 2911

rails 3: custom routes in routes.rb

I want to get link to user like: /chicago/123-olegpasko . In my helper:

def users_path(user)
  "/#{if user.city; user.city.name; else; "city";end}/#{user.to_param}"
end

How can I create a right routes?

Now I have something like:

match 'dontknow/:id' => 'users#show', :as => :users

Upvotes: 0

Views: 536

Answers (3)

Oleg Pasko
Oleg Pasko

Reputation: 2911

The solution was simple:

match ':name/:id' => 'users#show', :as => :users

Upvotes: 0

zolter
zolter

Reputation: 7160

Simple add this lines in you model:

def to_param
  "#{id}-#{name.parameterize}"
end

Upvotes: 1

Justin D.
Justin D.

Reputation: 4976

Check out the Railcasts about friendly_url

Also take a look at the routes casts

Upvotes: 1

Related Questions