Forum: Ruby on Rails Pagination with letter (A B C D ... Z)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Robert Z. (Guest)
on 2006-04-10 14:56
(Received via mailing list)
Hi there,

Is there a neat and easy way to implement pagination with letters
rather than numbers eg :

A B C D ... Z

Rob
Jamey C. (Guest)
on 2006-04-10 17:23
(Received via mailing list)
Robert Z. wrote:

> Hi there,
>
> Is there a neat and easy way to implement pagination with letters
> rather than numbers eg :
>
> A B C D ... Z

I've implemented somehting akin to this in my latest Rails app.  I have
a partial, called _filter.rhtml, that looks like this:

<div id="filterButtons">
<% LETTERS.each do |l| %>
 <%= if params[:filter] == l
      link_to(l, { :filter => l }, { :id => 'selectedFilterButton' })
     else
       link_to(l, :filter => l)
     end -%>
<% end %>
</div>

This creates links for all 26 letters across the top of my list page.

Then, in the controller, I use the :filter param value like so:

  def list
    params[:filter] = 'All' unless params.has_key?(:filter)

    if params[:filter] == 'All'
      @server_pages, @servers = paginate(:servers, :per_page => 10,
       :order_by => 'name')
    else
      @server_pages, @servers = paginate(:servers, :per_page => 10,
       :conditions => lambda { |r| r.name[0..0].upcase ==
params[:filter] },
       :order_by => 'name')
    end
  end

So, if a user clicks on "B", it will show all servers whose name begins
with B.  Note that in the :condtions hash value above, I am using a
lambda.  This is because I am using Ackbar/KirbyBase as the dbms.  If
you are using a regular dbms like MySQL, you would want to use a SQL
statement as your :conditions value.

HTH,

Jamey

Confidentiality Notice: This email message, including any attachments,
is for the sole use of the intended recipient(s) and may contain
confidential and/or privileged information. If you are not the intended
recipient(s), you are hereby notified that any dissemination,
unauthorized review, use, disclosure or distribution of this email and
any materials contained in any attachments is prohibited. If you receive
this message in error, or are not the intended recipient(s), please
immediately notify the sender by email and destroy all copies of the
original message, including attachments.
Kevin O. (Guest)
on 2006-04-10 17:38
(Received via mailing list)
I am a bit concerned that the direct use of the params hash in your
conditions would leave you open to SQL injection attacks.  Since you are
using a different database, it may be a non-issue, but in general it is
dangerous to put stuff from your params hash directly into your find
statements without some sort of sanitizing.

>params[:filter] },
>       :order_by => 'name')
>    end
>  end
>

_Kevin
Jamey C. (Guest)
on 2006-04-10 17:53
(Received via mailing list)
Well, this is an intranet app for my company, so I wasn't too concerned
about injection attacks when I wrote it, but you do have a good point.

Also, since the lambda is actually a Ruby code block, it wouldn't be a
SQL injection attack that would have to be used, but, again, in general,
your point is well taken.

Jamey

Kevin O. wrote:

>>
>>
>>
>>
>
>_Kevin
>
>
>


Confidentiality Notice: This email message, including any attachments,
is for the sole use of the intended recipient(s) and may contain
confidential and/or privileged information. If you are not the intended
recipient(s), you are hereby notified that any dissemination,
unauthorized review, use, disclosure or distribution of this email and
any materials contained in any attachments is prohibited. If you receive
this message in error, or are not the intended recipient(s), please
immediately notify the sender by email and destroy all copies of the
original message, including attachments.
Steve R. (Guest)
on 2006-04-11 03:00
(Received via mailing list)
How about

# your table and field names will vary...
@alpha = find_by_sql('select distinct substr(short_desc,1,1) from
listings
order by short_desc')

the resultant AR recordset provides only the letters used (why use the
whole
alphabet if only certain letters are used. Then, your pagination links
look
like:

<!-- crude attempt at link generation :) -->
<% @alpha.each do |a| %>
  <%= link_to(a, :action => show_page, :start_with => a) %>&nbsp;
<% end %>
--
View this message in context:
http://www.nabble.com/Pagination-with-letter-%28A-...
Sent from the RubyOnRails Users forum at Nabble.com.
Kyle M. (Guest)
on 2006-04-11 08:01
(Received via mailing list)
I'd just like to point out that

"A".upto("Z") do ...

is a lot better than

LETTERS = %w(A B C D ....
LETTERS.each do

--
Kyle M.
Chief Technologist
E Factor Media // FN Interactive
removed_email_address@domain.invalid
1-866-263-3261
Jamey C. (Guest)
on 2006-04-11 15:10
(Received via mailing list)
Kyle M. wrote:

>I'd just like to point out that
>
>"A".upto("Z") do ...
>
>is a lot better than
>
>LETTERS = %w(A B C D ....
>LETTERS.each do
>
>
Except for the fact that my LETTERS constant looks like this:

LETTERS = ['All', '0', '1', '2', ... 'X', 'Y', 'Z']

Because our server names can begin with a number and I also want to give
the user the ability to select all servers.

So, I probably should have picked a different name for the constant, but
I can't just use #upto like you suggest.

Jamey
Rimantas L. (Guest)
on 2006-04-11 15:29
(Received via mailing list)
<...>
> Except for the fact that my LETTERS constant looks like this:
>
> LETTERS = ['All', '0', '1', '2', ... 'X', 'Y', 'Z']

> So, I probably should have picked a different name for the constant, but
> I can't just use #upto like you suggest.

Some typing...

LETTERS = ['All',(0..1).to_a,('A'..'Z').to_a].flatten

Still not sure if this is most optimal way.


Regards,
Rimantas
--
http://rimantas.com/
Phillip H. (Guest)
on 2006-04-11 15:29
(Received via mailing list)
On 11/04/2006, at 11:10 PM, Jamey C. wrote:

>
> LETTERS = ['All', '0', '1', '2', ... 'X', 'Y', 'Z']
>
> Because our server names can begin with a number and I also want to
> give the user the ability to select all servers.
>
> So, I probably should have picked a different name for the
> constant, but I can't just use #upto like you suggest.

LETTERS = ['All']+("1".."9").to_a+("A".."Z").to_a

Avoids all that pesky typing ;)

--
Phillip H.
removed_email_address@domain.invalid
http://www.sitharus.com/
Rimantas L. (Guest)
on 2006-04-11 15:32
(Received via mailing list)
> LETTERS = ['All',(0..1).to_a,('A'..'Z').to_a].flatten

oops, (0..9).to_a, of course


Regards,
Rimantas
--
http://rimantas.com/
This topic is locked and can not be replied to.