Hello, I''d like to know if it''s possible to do something like this with routes: http://www.example.com/joeuser maps to controller => profiles, action => view, id => username Basically, what I''m trying to do is display a user''s profile based on their username entered in the URL. If its a real pain to do that, I''m willing to make it something like this as well: http://www.example.com/p/joeuser where p would map to the profiles controller I appreciate any help and sample routes would be great! thanks -- - Ramin http://www.getintothis.com/blog _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> > I''d like to know if it''s possible to do something like this with routes: > > http://www.example.com/joeuser > > maps to > > controller => profiles, action => view, id => username > > Basically, what I''m trying to do is display a user''s profile based on their > username entered in the URL. If its a real pain to do that, I''m willing to > make it something like this as well: > > http://www.example.com/p/joeuser > > where p would map to the profiles controller > > I appreciate any help and sample routes would be great! thanksVery simple actually: map.connect '':user'', :controller => ''profiles'', :action => ''view'' In the action, use @params[:user] to access that variable. However, you will run into problems because Rails will assume anything is a user name. If you have an Admin controller that you try to access at http://example.com/admin, it will assume admin is the username. Because of this, it''s usually better to use some kind of prefix, like in your second question. map.connect ''p/:user'', :controller => ''profiles'', :action => ''view'' The Routing manual has some good beginner info: http://manuals.rubyonrails.com/read/chapter/65 The Rails book has an even better chapter :). If you don''t plan on getting it, check out the routes files for some of the free apps out there. typo: http://typo.leetsoft.com/trac.cgi/file/trunk/config/routes.rb comiclog (mine): http://dev.comiclog.com/file/trunk/config/routes.rb I went a little crazy with my routing... -- rick http://techno-weenie.net
Thanks a bunches! =] On 5/27/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > > > I''d like to know if it''s possible to do something like this with routes: > > > > http://www.example.com/joeuser > > > > maps to > > > > controller => profiles, action => view, id => username > > > > Basically, what I''m trying to do is display a user''s profile based on > their > > username entered in the URL. If its a real pain to do that, I''m willing > to > > make it something like this as well: > > > > http://www.example.com/p/joeuser > > > > where p would map to the profiles controller > > > > I appreciate any help and sample routes would be great! thanks > > Very simple actually: > map.connect '':user'', :controller => ''profiles'', :action => ''view'' > In the action, use @params[:user] to access that variable. However, > you will run into problems because Rails will assume anything is a > user name. If you have an Admin controller that you try to access at > http://example.com/admin, it will assume admin is the username. > Because of this, it''s usually better to use some kind of prefix, like > in your second question. > > map.connect ''p/:user'', :controller => ''profiles'', :action => ''view'' > > The Routing manual has some good beginner info: > http://manuals.rubyonrails.com/read/chapter/65 > > The Rails book has an even better chapter :). If you don''t plan on > getting it, check out the routes files for some of the free apps out > there. > > typo: http://typo.leetsoft.com/trac.cgi/file/trunk/config/routes.rb > comiclog (mine): http://dev.comiclog.com/file/trunk/config/routes.rb > > I went a little crazy with my routing... > > -- > rick > http://techno-weenie.net >-- - Ramin http://www.getintothis.com/blog _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 5/27/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Because of this, it''s usually better to use some kind of prefix, like > in your second question. > > map.connect ''p/:user'', :controller => ''profiles'', :action => ''view''You could also do map.connect '':user'', :controller => ..., :user => /^~.*$/ Then you might want to use a before filter such as before_filter {|c| c.params[:user] = c.params[:user][1..-1] if c.params[:user]}