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 -- Posted via http://www.ruby-forum.com/.
David Smit 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) > endYou want @user = User.find_by_name(params[:user]) -- Posted via http://www.ruby-forum.com/.
Chris Carter wrote:> David Smit 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? -- Posted via http://www.ruby-forum.com/.
> Do I have to write a find_by_name function in the user Model?Nope! It''s magic. -- Posted via http://www.ruby-forum.com/.
> 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 -- Posted via http://www.ruby-forum.com/.
Stephen Bartholomew 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''. > > SteveThanks 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 -- Posted via http://www.ruby-forum.com/.