# Tag Search Showing, Best Method

**URL:** <https://www.ruby-forum.com/t/tag-search-showing-best-method/175029>\
**Category:** Rails\
**Created:** [September 6, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/tag-search-showing-best-method/175029 "2009-09-06T14:59:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darquirrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@Darquirrin](https://www.ruby-forum.com/u/Darquirrin)\
**Post date:** [September 6, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/tag-search-showing-best-method/175029/1 "2009-09-06T14:59:49Z")

</div>

First, please allow me to say I am very new to Rails and Ruby in  
general. I have only been working with it… For about one month. I’ve  
read every book and watched every screencast I could find on the  
subject and as a result I am finally nearing the end of my first rail  
app that will be going out live. But as you might guess, I’m having a  
problem.

Being so new I’m a bit lost when it comes where code should go at  
times and how exactly to handle RESTful resources.

Using the somewhat standard analogy of a blog, I have at the moment  
three models and controllers. users, user\_sessions, and posts (and the  
controllers there of, ie, user\_controller)

I am using acts\_as\_taggable\_on (for tagging), authlogic (user login  
behavior), mandraka-aegis(user roles) and white\_list ( sanitation of  
html from user’s post).

I have tagging working. Giving the relative complexities of tagging, I  
thought it would be a lot harder. However now, I want users to be able  
to ‘click’ on a tag for a given post and find all posts with matching  
tags. Now this is a trival thing, I can find them all easily however  
my issue is that I do not know how to display this list of posts  
RESTfully.

I know this will likely seem obvious to the senior members here but  
for me, it’s not a question I can seem to come up with an answer for.  
Should I create another new controller by itself to handle an Index or  
Show action of the list? A model and a controller? add a new non-REST  
custom method to the Post controller? Should I just recode the Post  
controller to handle either single or multiple post displays? What is  
the best method for redirecting to a list of these posts RESTfully?

I apologize ahead of time if this is a stupid question and a waste of  
your time but it has me stumped. Thank you for your time.

Darian

---

<div class="post-metadata">

**Author:** ![Darquirrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@Darquirrin](https://www.ruby-forum.com/u/Darquirrin)\
**Post date:** [September 6, 2009, 4:39pm UTC](https://www.ruby-forum.com/t/tag-search-showing-best-method/175029/2 "2009-09-06T16:39:20Z")

</div>

On Sep 6, 6:26 am, Darquirrin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Using the somewhat standard analogy of a blog, I have at the moment  
> tags. Now this is a trival thing, I can find them all easily however
> 
> I apologize ahead of time if this is a stupid question and a waste of  
> your time but it has me stumped. Thank you for your time.
> 
> Darian

I find fretting alot over whether or not one’s app conforms for  
RESTful “standards” really really misses the point. BUT typical  
RESTful guidelines say that a “show” request displays one of the  
requested resources, while “index” displays many of the requested  
resource. Sounds here that users are requesting many posts filtered by  
given tags, so _I’d_ just check the appropriate query parameters in  
your PostsController’s index action and go from there.

However I don’t really spend my day’s pondering the principles of REST  
so I may be completely off base here.

---

<div class="post-metadata">

**Author:** ![Darquirrin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d6d6ee/32.png) [@Darquirrin](https://www.ruby-forum.com/u/Darquirrin)\
**Post date:** [September 7, 2009, 11:13am UTC](https://www.ruby-forum.com/t/tag-search-showing-best-method/175029/3 "2009-09-07T11:13:09Z")

</div>

What I decided to do was to move .tagged\_width to the model and reuse  
the index with a slight tweak

in this manner:

# Posts\_Controller

def index  
#If the tags param is present, do by tags, if not, do all  
if params[:tags]  
@posts = Post.paginated\_tag\_search(params[:tags], params[:page])  
@tags = params[:tags]  
else  
@posts = Post.paginated\_all(params[:page])  
@tags = nil  
end  
end

# Post\_Model

## Find all with matching tags and paginate them

def self.paginated\_tag\_search(search, page)  
options = {  
:order =\> ‘created\_at DESC’,  
:page =\> page,  
:per\_page =\> 25  
}  
Post.tagged\_with(search, :include =\> [:tags, :user]).paginate  
(options)  
end

## Find ALL And Paginate them

def self.paginated\_all(page)  
options = {  
:order =\> ‘created\_at DESC’,  
:page =\> page,  
:per\_page =\> 25  
}  
Post.find(:all, :include =\> [:tags, :user]).paginate(options)  
end
