On Jan 25, 9:22 pm, Chris Johnson <[email protected]> wrote:
> You could just do that for the show method. Depending on your situation.
> If you were to pass the username in the query string just use a method like
> this:
>
> def show
> �...@user = User.find_by_username(params[:id])
> end
>
> -Chris
I think this is the right approach
take a look at the rails docs on to_param.
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M002024
So the right solution is
map.resources :users
class User
def to_param
self.username
end
end
class UsersControllers
def show
@user = User.find_by_username(params[:id])
end
def something_that_redirects
redirect_to users_path(@user)
end
end
Should suffice,
and is much better practice than overwriting the "find" method.
Enjoy.
Matthew Rudy
>
> On Sun, Jan 25, 2009 at 2:21 PM, Louis-Pierre Dahito <
>
> [email protected]> wrote:
>
> > Hey guys,
>
> > How do we create Permalinks for users in a Rails application ???
> > By Permalink a mean the name of the user in the URL like so:
> > example.com/username... Or like twitter and everyone does it ex:
> > twitter.com/username...
>
> > I checked everywhere for that and didnt find a relevant answer...
> > I think it has something to do with overriding the to_param method in
> > the User model... but according to this screencast from Ryan Bates,
> >http://railscasts.com/episodes/63-model-name-in-url, it's very risky to
> > remove the id from the url...
>
> > Is there a way to do it ??? I suppose there is...
>
> > Thx for your help...
>
> > Louis-Pierre
> > --
> > Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---