# Filtering results through URL (e.g. new?forum\_id=3)

**URL:** <https://www.ruby-forum.com/t/filtering-results-through-url-e-g-new-forum-id-3/143541>\
**Category:** Rails\
**Created:** [July 20, 2008, 8:03am UTC](https://www.ruby-forum.com/t/filtering-results-through-url-e-g-new-forum-id-3/143541 "2008-07-20T08:03:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [July 20, 2008, 8:03am UTC](https://www.ruby-forum.com/t/filtering-results-through-url-e-g-new-forum-id-3/143541/1 "2008-07-20T08:03:44Z")

</div>

Is there a special code you have to place in the routes so filtering  
results through the URL works?

For example, Ruby Forum does it with:

[http://www.ruby-forum.com/topic/new?forum\_id=3](http://www.ruby-forum.com/topic/new?forum_id=3)  
[http://www.ruby-forum.com/topic/new?forum\_id=5](http://www.ruby-forum.com/topic/new?forum_id=5)  
[http://www.ruby-forum.com/topic/new?forum\_id=7](http://www.ruby-forum.com/topic/new?forum_id=7)  
etc.

Any ideas?

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [July 20, 2008, 11:09am UTC](https://www.ruby-forum.com/t/filtering-results-through-url-e-g-new-forum-id-3/143541/2 "2008-07-20T11:09:01Z")

</div>

Hi Bob,

It seems that the example you gave may be doing something different  
that what you describe. I believe the forum\_id will be used as a value  
in a new topic form, not as a “filter” for a list of topics.

You might look at this:  
[http://api.rubyonrails.org/classes/ActionController/Resources.html#M000696](http://api.rubyonrails.org/classes/ActionController/Resources.html#M000696)  
Specifically the part about routing association.

If you wanted to “filter” a list of topics by forum\_id you might do  
something like the following:

# routes.rb

map.resources :forums do |forums|  
forums.resources :topics  
end

This would generate routes like forum\_topics and new\_forum\_topic so  
you could call the following in your controllers & views:

# some\_view.html.erb

\<%= link\_to “click here to see #{@my\_forum.name} topics”,  
forum\_topics\_path(@my\_forum) %\>

This would result in a link to /forums/7/topics where @my\_forum’s id  
is 7.

For a complete list of routes you’ve defined try this in your command  
line:  
$ rake routes

When GETing the path above, your topics\_controller index action will  
be called. In that method you’ll need to look for params[:forum\_id]  
when finding your list of topics for the desired forum:

# topics\_controller

def index  
if params[:forum\_id]  
#find topics belonging to a specified forum  
@forum = Forum.find(params[:forum\_id])  
@topics = @forum.topics  
else  
# find all topics  
@topics = Topic.find(:all)  
end  
…  
end

Good luck.

\*untested code examples

On Jul 20, 2:03 am, Bob S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![adistarmid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f14d63/32.png) [@adistarmid](https://www.ruby-forum.com/u/adistarmid)\
**Post date:** [July 20, 2008, 12:43pm UTC](https://www.ruby-forum.com/t/filtering-results-through-url-e-g-new-forum-id-3/143541/3 "2008-07-20T12:43:54Z")

</div>

Really appreciate the help, gsterndale! =)

Very detailed. Thank you so much!
