Ruby on rails 调整控制器中方法调用的routes.rb

Ruby on rails 调整控制器中方法调用的routes.rb,ruby-on-rails,Ruby On Rails,我在Rails中创建了一个在线投资组合,其中包含不同的项目。我想能够过滤关键字的项目。我的方法是为ProjectsController中的每个关键字定义一个方法,并链接这些关键字以调用这些方法 例如,关键字=图形设计: <%= link_to 'Graphic Design', :action => "filter_"+@project.keyword.to_s %> 这对我来说是很明显的。我的问题:有没有一种方法可以告诉routes.rb只对“filter_u2;”方法有

我在Rails中创建了一个在线投资组合,其中包含不同的项目。我想能够过滤关键字的项目。我的方法是为ProjectsController中的每个关键字定义一个方法,并链接这些关键字以调用这些方法

例如,关键字=图形设计:

<%= link_to 'Graphic Design',  :action => "filter_"+@project.keyword.to_s %>

这对我来说是很明显的。我的问题:有没有一种方法可以告诉routes.rb只对“filter_u2;”方法有不同的行为?还有其他建议吗?

我想这样的建议行得通

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

虽然你的方法是错误的,但我还没有试过。为什么您首先需要为每个关键字使用筛选方法?这是一个非常简单的解决方案。首先在routes.rb中定义命名路由:

map.filter '/projects/:filter_this_for_me', :controller => 'projects', :action => 'filter'
在你看来,

<%= link_to 'Graphic Design',  filter_path("filter_" + @project.keyword.to_s) %>
<%= link_to 'Graphic Design',  filter_path("filter_" + @project.keyword.to_s) %>
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