views:

28

answers:

2

I have a users controller in my application, routed with:

map.resources :users

This has my user pages living at /users/1, and so forth.

I'd like my user pages to live at /users/blake, etc.

What's the right way to do this in rails, such that I can say link_to(@user) and the correct path is generated?

+1  A: 

In model:

class User < ActiveRecord::Base
  def to_param
    login
  end
end

In controller:

class UsersController < ApplicationController
  def show
    @user = User.find_by_login(params[:id])
    #...
  end
end

to_param in model is used by ActionPack to construct url for this object. And in controller you need to fetch your model by this field.

MBO
+1  A: 

This article is a nice tutorial on friendly URL's

Maulin