8571728785e5e350ddb58cd458533b12

There's gotta be a much better way for this all around. This works at the moment though. Should be straightforward enough. This is using the will_paginate stylesheet for digg style navigation, but for these links, which is where the class names and markup come from. Any suggestions on getting this array declaration down? Or anything else you see?

1
2
3
4
5
6
7
8
9
10
<div class="pagination">
  <% letters = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','#'] -%>
  <% for letter in letters -%>
    <% if params[:search] == letter -%>
      <span class="current"><%= letter %></span>
    <% else -%>
      <%= link_to letter, :controller => 'controller', :search => letter %>
    <% end -%>
  <% end -%>
</div>

Refactorings

No refactoring yet !

1eecfce54b4f902784d046328935efd4

Makoto

December 16, 2007, December 16, 2007 09:43, permalink

No rating. Login to rate!

How about this?

1
2
3
4
5
6
7
8
9
10
11
12
13
#a view
<% LETTERS = ('A'..'Z').to_a << '#' %>
<div class="pagination">
   <%= render(:partial => "letter", :collection => LETTERS)%>
</div>

#_letter.rhtml

<% if params[:search] == letter %>
  <span class="current"><%= letter %></span>
<% else %>
  <%= link_to letter, :controller => 'controller', :search => letter %>
<% end %>
D16d53391068ff0830269149b060789d

Jason Dew

December 17, 2007, December 17, 2007 00:25, permalink

No rating. Login to rate!

I don't really like having to define a variable inside your view, so I would move the code into a helper.

Helper

1
2
3
4
5
6
7
8
9
10
11
12
LETTERS = ('A'..'Z').entries + ['#']

def navigation_links(current_letter)
  LETTERS.each do |letter|
    if letter == current_letter
      content_tag :span, letter, :class => 'current'
    else
      link_to letter, :controller => 'controller', :search => letter
    end
  end
end

View

1
2
3
4
<div class="pagination">
  <%= navigation_links params[:search] %>
</div>
2d356b56308f9db140af8ccbf9b8f28f

Jermaine

December 30, 2007, December 30, 2007 18:21, permalink

No rating. Login to rate!

Hi Jason,
It doesn't work for me....? For some reason I don't get the links (a hrefs.....)

D16d53391068ff0830269149b060789d

Jason Dew

December 30, 2007, December 30, 2007 19:22, permalink

No rating. Login to rate!

Apologies, I wrote this code without testing it ... Please see the updated helper code and rate if you found it useful. Thanks!

helper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
LETTERS = ('A'..'Z').entries + ['#']

def navigation_links(current_letter)
  returning [] do |html|
    LETTERS.each do |letter|
      if letter == current_letter
        html << content_tag( :span, letter, :class => 'current' )
      else
        html << link_to( letter, :controller => 'controller', :search => letter )
      end
    end
  end.join
end
2d356b56308f9db140af8ccbf9b8f28f

Jermaine

December 30, 2007, December 30, 2007 19:41, permalink

No rating. Login to rate!

Hi Jason,
Thanks 4 your fast reply....
It looks good though, but I think you're missing something (I can't figure it out). Because now I don't get output at all out of <%= navigation_links params[:search] %>

D16d53391068ff0830269149b060789d

Jason Dew

December 30, 2007, December 30, 2007 20:45, permalink

No rating. Login to rate!

I did leave out a piece (the html << in front of link_to). What kind of values are you giving to params[:search]?

2d356b56308f9db140af8ccbf9b8f28f

Jermaine

December 30, 2007, December 30, 2007 20:51, permalink

No rating. Login to rate!

Jason...Da Shhh***t
Again thanks 4 the fast reply.
This worked great for me! Many thanks.
Values to params[:search]? well.. just the letters.. (i.e.: A, B, C D etc..)

Avatar

starshine

November 20, 2008, November 20, 2008 17:17, permalink

No rating. Login to rate!

Thanks so much for this! I changed the helper up a bit, like changed the code to be able to work with multiple controllers in the App Helper and changed the params from search to letter since my filter already includes a search box.

Many many many thanks!

Helper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  LETTERS = ('A'..'Z').entries + ['All']
  
  def navigation_links(current_letter, controller)
    returning [] do |html|
      LETTERS.each do |letter|
        if letter == current_letter
          html << ' '+ content_tag( :span, letter, :class => 'current' )
        else
          html << ' '+ link_to( letter, :controller => controller, :letter => letter )
        end
      end
    end.join
  end

View

1
2
  .pagination
    = navigation_links params[:letter], 'organizations'

Your refactoring





Format Copy from initial code

or Cancel