Filtering results through URL (e.g. new?forum_id=3)

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:

Any ideas?

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:
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:


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

This would generate routes like forum_topics and new_forum_topic so
you could call the following in your controllers & views:


<%= link_to “click here to see #{} 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
$ 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:


def index
if params[:forum_id]
#find topics belonging to a specified forum
@forum = Forum.find(params[:forum_id])
@topics = @forum.topics
# find all topics
@topics = Topic.find(:all)


Good luck.

*untested code examples

On Jul 20, 2:03 am, Bob S. [email protected]

Really appreciate the help, gsterndale! =)

Very detailed. Thank you so much!

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