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 via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 < rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok... I''ll try that then... Thx a lot for helping... Louis-Pierre -- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 25, 9:22 pm, Chris Johnson <ch...-dAXfyQEfk+W1Z/+hSey0Gg@public.gmane.org> 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 > > -ChrisI 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 < > > rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You don''t need to do self.username, just username will suffice On 26/01/2009, at 20:42, MatthewRudy <matthewrudyjacobs-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > On Jan 25, 9:22 pm, Chris Johnson <ch...-dAXfyQEfk+W1Z/+hSey0Gg@public.gmane.org> 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 < >> >> rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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-+evGEzgkreAAvxtiuMwx3w@public.gmane.org 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
map.connect '':username'', :controller => "users" , :action => "show" At the bottom of your routes.rb file. ----- Ryan Bigg On 27/01/2009, at 1:07 PM, Louis-Pierre Dahito wrote:> > > > 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 > -- > Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. On Mon, Jan 26, 2009 at 9:13 PM, Ryan Bigg <radarlistener-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > map.connect '':username'', :controller => "users" , :action => "show" > > At the bottom of your routes.rb file. > ----- > Ryan Bigg > > > > > > > > On 27/01/2009, at 1:07 PM, Louis-Pierre Dahito wrote: > >> >> >> >> 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 >> -- >> Posted via http://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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---