Acts_as_taggable and paginate?

Hi there,

I’ve been trying to paginate over a list of members that all share a tag
in
common using the acts_as_taggable plugin. The regular way of paginating
over
a collection doesn’t seem to work with acts_as_taggable. Here’s what my
method looks like that takes in a tag name, finds all the members that
share
the tag and then displays all the members. Nothing too fancy at the
moment…

def show_all_tags
tag_name = params[:id]
@tagged_members = Tag.find_by_name(tag_name).tagged
end

Doing the standard: @tagged_member_pages, @tagged_members = paginate
:member, :per_page => 12 displays ALL the members in the DB.

Has anyone conquered such an issue like this?

Thank you,
Dave H.

On Tuesday, June 20, 2006, at 9:55 PM, Dave H. wrote:

Thank you,
Dave H.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

You want to do something like this…

def list
if params[:tag] then
@tags = params[:tag].split.map {|o| CGI::unescape(o)}
@items = Item.find_tagged_with(@tags)
@item_pages = Paginator.new(ItemController, @items.size, 10)
else
@item_pages, @items = paginate(:item, :order=>:name)
end
end

just make sure your routes are set up for the :tag option…

map.connect ‘:controller/:action/:tag’, :action => ‘index’,:tag=>nil,

_Kevin

Hi Kevin,

Thank you for the tips. I tried out what you suggested and it works as
expected, accpet for one glitch in it. Here’s what my code looks like
now:

def show_all_tags
if params[:id] then
@tags = params[:id].split.map {|o| CGI::unescape(o)}
@members = Member.find_tagged_with(@tags)
@member_pages = Paginator.new(Member, @members.size, 2)
else
@member_pages, @members = paginate(:member)
end
end

…and in the view:

<%= pagination_links(@member_pages) %>
<% for member in @members %> ....do stuff.... <% end %>

I’m still getting all the members showing up, even if I limit the view
to 2
members per page. The paginate links show up correctly depending on how
many
members I designate to show, but the collection of members doesn’t obey
the
pagination. I have implemented pagination to work correctly in other
areas
of my app, but using the acts_as_taggable plugin (and methods) may very
well
be the culprit here. I really have no idea!

Any other suggestions?

Thanks!
-Dave

On 21 Jun 2006 03:17:27 -0000, Kevin O. <

Hi Kevin,

I put the raise ‘test’ in before the paginate function, and when I run
it I
do see “test” in my browser. I also put a redirect to a general list
action
if no params come through. The “else” part of the block does work
correctly.

def show_all_tags
if params[:id] then
@tag_title = params[:id]
@tags = params[:id].split.map {|o| CGI::unescape(o)}
@members = Member.find_tagged_with(@tags)
raise ‘test’
@members_pages = Paginator.new(Member, @members.size, 2)
else
redirect_to :action => ‘list’
end
end

My route for this action looks like this:

map.connect ‘tags/:id’, :controller => ‘member’, :action =>
‘show_all_tags’

This URL does display members with a common tag:
http://localhost:3000/tags/photographer

This URL does redirect back to a “list” action if no tag is present
http://localhost:3000/tags/

-Dave

On 21 Jun 2006 17:24:07 -0000, Kevin O. <

I found the following bit of code here (on the bottom of the page):
http://wiki.rubyonrails.org/rails/pages/ActsAsTaggablePluginHowto/versions/59

module ActionController
module Pagination
MY_OPTIONS = {:tag => ‘All’}
DEFAULT_OPTIONS.merge!(MY_OPTIONS) {|key, old, new| old}

 alias old_find_collection_for_pagination 

find_collection_for_pagination

 def find_collection_for_pagination(model, options, paginator)
   models = old_find_collection_for_pagination(model, options,

paginator)
if options[:tag]==“All”
models
else
models.delete_if {|m| !m.is_tagged_with?(options[:tag])}
end
end
end
end

…but really have no idea WHERE to put it in the acts_as_taggable.rb
file.
How would you actually use something like this? Do you think this is a
viable option?

Here’s the description of it:
Note Acts_as_taggeble with pagination
The following opens the pagination class back up adds the abilty to
filter
based on a single tag. Pass a :tag => “tag” and the pagination with be
base
on though s items that are tagged with it. Belongs in
acts_as_taggable.rb.
EO

-Dave

On Wednesday, June 21, 2006, at 10:28 AM, Dave H. wrote:

else
<% for member in @members %>
be the culprit here. I really have no idea!

that share

end
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

It looks like your action is falling through to the else clause.
throw line like this in these before the paginate function

raise ‘test’

If it generates an error when you hit the action, then it’s not pulling
the id out properly.

What does the url you are accessing the action with look like?
What do your routes look like?

_Kevin

Hi Kevin (again),

I tried the MemberController change and that still didn’t work.

However… good news… I did solve it with the following code:

def show_all_tags
per_page = 12
if params[:id] then
@tag_title = params[:id]
@tags = params[:id].split.map {|o| CGI::unescape(o)}
@members = Member.find_tagged_with(@tags)
@members_pages, @members = paginate(Member, {:conditions => [’
tags.name = ?’, @tag_title], :include => [:tags], :per_page =>
per_page})
else
redirect_to :action => ‘list’
end
end

Thank you for all your help!
-Dave H.

On 21 Jun 2006 20:15:10 -0000, Kevin O. <

On Wednesday, June 21, 2006, at 1:38 PM, Dave H. wrote:

 @tag_title = params[:id]

My route for this action looks like this:

now:

I’m still getting all the members showing up, even if I limit the
be the culprit here. I really have no idea!

my

Doing the standard: @tagged_member_pages, @tagged_members =
Rails mailing list
else

[email protected]

http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

I think your line like this…

 @member_pages = Paginator.new(Member, @members.size, 2)

should actually be

 @member_pages = Paginator.new(MemberController, @members.size, 2)

_Kevin