Pretty URLs -> Routes

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
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
should route to or act the same as:
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

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])

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?

Do I have to write a find_by_name function in the user Model?
Nope! It’s magic.

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:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html under ‘Dynamic
attribute-based finders’.

Steve

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:
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/

Thanks

David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs