I would like to have a users page where one could list all users, paginated, or select A-Z to view only those users whose last name begins with the selected letter. My initial thoughts on how to do this involves using the method_missing method in the controller. When invoked, if the message sent is a single letter, A-Z, it would basically do a slightly customized find to get the appropriate records. Is this a sane approach, or is there something simpler available? I am figuring yes, since this is not a terribly uncommon task. Thanks!
Seth Rasmussen
2005-Sep-22 03:21 UTC
Re: List users by last name, all or one letter at a time
> My initial thoughts on how to do this involves using the > method_missing method in the controller.Err, that should be in the model, not the controller... but yes, same idea, I think.
Seth Rasmussen
2005-Sep-22 03:34 UTC
Re: List users by last name, all or one letter at a time
[...]
Okay, Seth, slow down... let me try this again. (Sorry, I''m not trying
to spam, I just want to be clear for anybody kind enough to try to
help.)
I want the scaffolded list method:
def list
@user_pages, @users = paginate :user, :per_page => 10
end
To do just what it already does, but if the :id passed to it is a
single letter, instead of a numeric value, I want to fetch only users
whose last name starts with that letter, but still paginate and all
that good stuff.
Okay, I''ll just wait now and continue reading the documentation. :)
Brad Ediger
2005-Sep-22 12:24 UTC
Re: Re: List users by last name, all or one letter at a time
I did the same thing for a member list (for a local chamber of
commerce). Here''s my controller code for this action: (It was a 100-
line application so putting all this in the controller wasn''t such a
Bad Thing.) I guess the find should be refactored into the model at
some point.
I have a separate action, list, that shows and paginates all results.
I also filter by category. The URLs work like this:
http://memberlist.siloamchamber.com/ (=> /list)
http://memberlist.siloamchamber.com/letter/A
See the API docs for pagination, under "Custom/classic pagination".
def letter
@member_pages = Paginator.new self, Member.count(["name like ?",
"#{@params[:id]}%"]), 10, @params[''page'']
@members = Member.find(:all, :conditions => ["name like ?",
"#
{@params[:id]}%"], :order => ''name'', :offset =>
@member_pages.current.to_sql[1], :limit =>
@member_pages.current.to_sql[0])
render(:action => ''list'')
end
--
Brad Ediger
866-EDIGERS
On Sep 21, 2005, at 10:34 PM, Seth Rasmussen wrote:
> [...]
>
> Okay, Seth, slow down... let me try this again. (Sorry, I''m not
trying
> to spam, I just want to be clear for anybody kind enough to try to
> help.)
>
> I want the scaffolded list method:
>
> def list
> @user_pages, @users = paginate :user, :per_page => 10
> end
>
> To do just what it already does, but if the :id passed to it is a
> single letter, instead of a numeric value, I want to fetch only users
> whose last name starts with that letter, but still paginate and all
> that good stuff.
>
> Okay, I''ll just wait now and continue reading the documentation.
:)
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
Derek Haynes
2005-Sep-22 15:31 UTC
Re: Re: List users by last name, all or one letter at a time
Seth,
This code might be helpful - Duane Johnson was the one who originally
posted it. It cleans up the process for pagination in a collection:
def paginate_collection(collection, options = {})
default_options = {:per_page => 10, :page => 1}
options = default_options.merge options
pages = Paginator.new self, collection.size,
options[:per_page], options[:page]
first = pages.current.offset
last = [first + options[:per_page], collection.size].min
slice = collection[first...last]
return [pages, slice]
end
- Derek
On 9/22/05, Brad Ediger <brad-sod+mMc99o6+XT7JhA+gdA@public.gmane.org>
wrote:> I did the same thing for a member list (for a local chamber of
> commerce). Here''s my controller code for this action: (It was a
100-
> line application so putting all this in the controller wasn''t such
a
> Bad Thing.) I guess the find should be refactored into the model at
> some point.
>
> I have a separate action, list, that shows and paginates all results.
> I also filter by category. The URLs work like this:
>
> http://memberlist.siloamchamber.com/ (=> /list)
> http://memberlist.siloamchamber.com/letter/A
>
> See the API docs for pagination, under "Custom/classic
pagination".
>
> def letter
> @member_pages = Paginator.new self, Member.count(["name like
?",
> "#{@params[:id]}%"]), 10, @params[''page'']
> @members = Member.find(:all, :conditions => ["name like
?", "#
> {@params[:id]}%"], :order => ''name'', :offset
=>
> @member_pages.current.to_sql[1], :limit =>
> @member_pages.current.to_sql[0])
> render(:action => ''list'')
> end
>
> --
> Brad Ediger
> 866-EDIGERS
>
>
> On Sep 21, 2005, at 10:34 PM, Seth Rasmussen wrote:
>
> > [...]
> >
> > Okay, Seth, slow down... let me try this again. (Sorry, I''m
not trying
> > to spam, I just want to be clear for anybody kind enough to try to
> > help.)
> >
> > I want the scaffolded list method:
> >
> > def list
> > @user_pages, @users = paginate :user, :per_page => 10
> > end
> >
> > To do just what it already does, but if the :id passed to it is a
> > single letter, instead of a numeric value, I want to fetch only users
> > whose last name starts with that letter, but still paginate and all
> > that good stuff.
> >
> > Okay, I''ll just wait now and continue reading the
documentation. :)
> > _______________________________________________
> > Rails mailing list
> > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
>
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Derek Haynes
HighGroove Studios - http://www.highgroove.com
Keeping it Simple.
404.593.4879