# Pretty URLs -\> Routes

**URL:** <https://www.ruby-forum.com/t/pretty-urls-routes/96860>\
**Category:** Rails\
**Created:** [July 2, 2006, 8:20pm UTC](https://www.ruby-forum.com/t/pretty-urls-routes/96860 "2006-07-02T20:20:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 2, 2006, 8:20pm UTC](https://www.ruby-forum.com/t/pretty-urls-routes/96860/1 "2006-07-02T20:20:01Z")

</div>

Hi

I am having issues with getting my pretty urls to work.

routes.rb:  
map.connect ‘:user’ , :controller =\> ‘front’ ,  
:action =\> ‘list’ ,  
:filter =\> ‘user’

front\_controller.rb:  
def list  
@advert\_pages, @adverts = paginate :adverts, :per\_page =\> 10

@user = User.find(params[:id])  
@userAdverts = @user.adverts.find(:all)  
end

[http://localhost:3009/dawie](http://localhost:3009/dawie)  
where dawie is one of the users

gives me an error: Couldn’t find User without an ID

which makes sense.

My problem is that I don’t know how to set up my controller so it knows  
that  
[http://localhost:3009/dawie](http://localhost:3009/dawie)  
should route to or act the same as:  
[http://localhost:3009/front/list/12](http://localhost:3009/front/list/12)

Do I have to change the way my list function works? So it doesn’t need  
the parameter. How does the userId(12) get mapped to the  
username(dawie)?

David

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 2, 2006, 8:21pm UTC](https://www.ruby-forum.com/t/pretty-urls-routes/96860/2 "2006-07-02T20:21:49Z")

</div>

David S. wrote:

> routes.rb:  
> map.connect ‘:user’ , :controller =\> ‘front’ ,  
> :action =\> ‘list’ ,  
> :filter =\> ‘user’
> 
> front\_controller.rb:  
> def list  
> @advert\_pages, @adverts = paginate :adverts, :per\_page =\> 10
> 
> @user = User.find(params[:id])  
> @userAdverts = @user.adverts.find(:all)  
> end

You want @user = User.find\_by\_name(params[:user])

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 2, 2006, 8:26pm UTC](https://www.ruby-forum.com/t/pretty-urls-routes/96860/3 "2006-07-02T20:26:06Z")

</div>

Chris C. wrote:

> David S. wrote:
> 
> > routes.rb:  
> > map.connect ‘:user’ , :controller =\> ‘front’ ,  
> > :action =\> ‘list’ ,  
> > :filter =\> ‘user’
> > 
> > front\_controller.rb:  
> > def list  
> > @advert\_pages, @adverts = paginate :adverts, :per\_page =\> 10
> > 
> > @user = User.find(params[:id])  
> > @userAdverts = @user.adverts.find(:all)  
> > end
> 
> You want @user = User.find\_by\_name(params[:user])

Do I have to write a find\_by\_name function in the user Model?

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 2, 2006, 9:01pm UTC](https://www.ruby-forum.com/t/pretty-urls-routes/96860/4 "2006-07-02T21:01:05Z")

</div>

> Do I have to write a find\_by\_name function in the user Model?  
> Nope! It’s magic.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 2, 2006, 9:02pm UTC](https://www.ruby-forum.com/t/pretty-urls-routes/96860/5 "2006-07-02T21:02:59Z")

</div>

> Do I have to write a find\_by\_name function in the user Model?  
> If you have a ‘name’ column, ‘find\_by\_name’ will work automatically -  
> it’s a dynamic finder and is a neat feature of active record.

Have a look at:  
[ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html) under ‘Dynamic  
attribute-based finders’.

Steve

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [July 2, 2006, 9:09pm UTC](https://www.ruby-forum.com/t/pretty-urls-routes/96860/6 "2006-07-02T21:09:09Z")

</div>

Stephen B. wrote:

> > Do I have to write a find\_by\_name function in the user Model?  
> > If you have a ‘name’ column, ‘find\_by\_name’ will work automatically -  
> > it’s a dynamic finder and is a neat feature of active record.
> 
> Have a look at:  
> [ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html) under ‘Dynamic  
> attribute-based finders’.
> 
> Steve

Thanks guys. This is an awsome forum. My issue was that I had to use  
find\_by\_login (because my db colum is called login and not name)

This blog post really helped me:  
[http://quirks.exposured.com/ruby/find\_by\_-methods-in-rails/](http://quirks.exposured.com/ruby/find_by_-methods-in-rails/)

Thanks

David
