Tag Search Showing, Best Method

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

On Sep 6, 6:26 am, Darquirrin [email protected] 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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs