And if you DO use to_param and do something like this:

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

And then go to the URL such as /users/15-Radar

In that show action you can then do:

User.find(params[:id])

still, and get the user. This is because the find calls to_i on the  
string that is passed into it, so it is equivalent to doing  
User.find(15).

-----
Ryan Bigg
Mocra - Premier iPhone and Ruby on Rails Consultants
w - http://mocra.com
e - ra...@mocra.com
p - +61 432 937 289 or +61 7 3102 3237
skype - radarlistener

On 27/01/2009, at 11:59 AM, Brian Hogan wrote:

>
> LOL
> There's no reason to use to_param.  It's just a convention to pass the
> id through the URL. It's type is not enforced.
>
> # expects /users/15
> def edit
>  @user = User.find(params[:id])
> end
>
> # expects /users/bphogan
> def show
>  @user = User.find_by_username(params[:id])
> end
>
> The only thing to watch out for is that find throws a RecordNotFound
> exception when there is no record found, which will display a 404 in
> production, and find_by_username returns nil, so you'll wanna handle
> that.
>
> But there's no need to do anything funny with to_params or routing or
> anything like that.
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to