views:

23

answers:

2

I have an online portfolio created in Rails featuring different projects. I want to be able to filter the projects by keywords. My approach is to define a method for each keyword in the ProjectsController and link the keywords to call the methods.

For Example Keyword = graphic_design:

<%= link_to 'Graphic Design',  :action => "filter_"[email protected]_s %>

That way I get the error:

Couldn't find Project with ID=filter_graphic_design

This is quite obvious to me. My question: Is there a way to tell the routes.rb to behave differently only for 'filter_' methods? Any other suggestions?

A: 

I think something like this could work

map.connect "/projects/filter_{filter}", :controller => 'projects', :action => 'filter'

Haven't tried it though

Jongsma
A: 

Your approach is wrong. Why do you need a filter_ method for each keyword in the first place? Its pretty simple a solution. First define a named route in your routes.rb:

map.filter '/projects/:filter_this_for_me', :controller => 'projects', :action => 'filter'

In your views,

<%= link_to 'Graphic Design',  filter_path("filter_" + @project.keyword.to_s) %>

In your filter action,

def filter
  logger.info("Parameters that is being received: #{params}")
  filter_what = params[:filter_this_for_me]

  if(!filter_what.nil? && !filter_what.blank?)
        # Here filter_what will have "filter_graphic_design" or "filter_something"
        # With which you can filter any data that you want.
        # Filter your projects here.
  end
end
Shripad K