Forum: Ruby on Rails User Permalink in Rails ???

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
56d11c3a51c280f6560424bb267076d8?d=identicon&s=25 Louis-Pierre Dahito (lpdahito)
on 2009-01-25 21:21
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
F540a9fb28d78dc66fa7f8f529822185?d=identicon&s=25 Chris Johnson (Guest)
on 2009-01-25 22:22
(Received via mailing list)
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

On Sun, Jan 25, 2009 at 2:21 PM, Louis-Pierre Dahito <
A91bd6cef23eb3516245a092e196c4da?d=identicon&s=25 Maurício Linhares (mauricio)
on 2009-01-25 22:32
(Received via mailing list)
Well, I haven't watched the screencast, but can't really imagine how
"harmful" that could be.

The first thing you would need to do is use the permalink_fu plugin ->
http://github.com/technoweenie/permalink_fu/tree/master

And then you could override the "find" method in your model to a
finder like this one:

    def find( *args )

      if args.first.kind_of?( String ) and !args.first.kind_of?(
Symbol ) and (args.first !~ /^[0-9]/ || args.first.include?('-'))
        if args.size == 1
          super :first, :conditions => { permalink_field => args.first }
        else
          with_scope :find => { :conditions => { permalink_field =>
args.first } } do
              args[ 0 ] = :first
              super( *args )
          end
        end
      else
        super( *args )
      end

    end

What would assure that if a number is passed, it would use the good
old finder, but if it's not a number, it will try to find using the
permalink_field. This has solved my needs, maybe it can solve yours
too.

-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/
(en)



On Sun, Jan 25, 2009 at 5:21 PM, Louis-Pierre Dahito
56d11c3a51c280f6560424bb267076d8?d=identicon&s=25 Louis-Pierre Dahito (lpdahito)
on 2009-01-26 07:00
Ok... I'll try that then... Thx a lot for helping...

Louis-Pierre
E60b2dc57668b5662ce3f07781e41710?d=identicon&s=25 Matthew Rudy Jacobs (matthewrudy)
on 2009-01-26 11:42
(Received via mailing list)
On Jan 25, 9:22 pm, Chris Johnson <ch...@johnsonch.com> 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/Ba...

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
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2009-01-26 13:41
(Received via mailing list)
You don't need to do self.username, just username will suffice



On 26/01/2009, at 20:42, MatthewRudy <matthewrudyjacobs@gmail.com>
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2009-01-27 03:00
(Received via mailing list)
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.
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2009-01-27 03:59
(Received via mailing list)
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 - radar@mocra.com
p - +61 432 937 289 or +61 7 3102 3237
skype - radarlistener
56d11c3a51c280f6560424bb267076d8?d=identicon&s=25 Louis-Pierre Dahito (lpdahito)
on 2009-01-27 04:07

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.

Thx a lot but I really do want to get rid of the /user before "username"
so my url looks like this: http://mydomain.com/username... like i said
before...
http://twitter.com/gigaom for instance... How do i get rid of "user/" in
the url ????

thx again
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2009-01-27 04:14
(Received via mailing list)
map.connect ':username', :controller => "users" , :action => "show"

At the bottom of your routes.rb file.
-----
Ryan Bigg
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2009-01-27 21:10
(Received via mailing list)
If you do
> map.connect ':username', :controller => "users" , :action => "show"

you'll need to look for params[:username] instead of params[:id] when
you look up the record.
This topic is locked and can not be replied to.