Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 轨道中的路线链接?_Ruby On Rails - Fatal编程技术网

Ruby on rails 轨道中的路线链接?

Ruby on rails 轨道中的路线链接?,ruby-on-rails,Ruby On Rails,我正在用Rails编写一个会议列表网站,遇到了以下要求: 链,无特定顺序,用于查找事件的URL,例如: /in/:city/on/:tag/with/:speaker 或者像这样重新排列 /在/:城市/带/:扬声器/打开/:标签 我可以一个接一个地处理这些问题。有没有一种方法可以动态处理这些请求?我是通过以下方法实现的(请原谅其中的一些特定于应用程序的代码): routes.rb: map.connect '/:type/*facets', :controller => 'events

我正在用Rails编写一个会议列表网站,遇到了以下要求:

链,无特定顺序,用于查找事件的URL,例如:

/in/:city/on/:tag/with/:speaker

或者像这样重新排列

/在/:城市/带/:扬声器/打开/:标签


我可以一个接一个地处理这些问题。有没有一种方法可以动态处理这些请求?

我是通过以下方法实现的(请原谅其中的一些特定于应用程序的代码):

routes.rb:

  map.connect '/:type/*facets', :controller => 'events', :action => 'facets'
事件\u controller.rb:

def facets
    @events = find_by_facets(params[:facets], params[:type])
    render :action => "index"

  end
def find_by_facets(facets, type = nil)
query = type.nil? ? "Event" : "Event.is('#{type.singularize.capitalize}')"
for i in (0..(facets.length - 1)).step(2)
  query += ".#{facets[i]}('#{facets[i+1].to_word.capitalize_words}')"
end
@events = eval(query)

end
应用程序_controller.rb:

def facets
    @events = find_by_facets(params[:facets], params[:type])
    render :action => "index"

  end
def find_by_facets(facets, type = nil)
query = type.nil? ? "Event" : "Event.is('#{type.singularize.capitalize}')"
for i in (0..(facets.length - 1)).step(2)
  query += ".#{facets[i]}('#{facets[i+1].to_word.capitalize_words}')"
end
@events = eval(query)

end
event.rb:

  named_scope :in, lambda { |city| { :conditions => { :location => city } } }

  named_scope :about, lambda {
      |category| {
          :joins => :category,
          :conditions => ["categories.name = ?", category]
      }
  }

  named_scope :with, lambda {
      |speaker| {
          :joins => :speakers,
          :conditions => ["speakers.name = ?", speaker]
      }
  }

  named_scope :on, lambda {
      |tag| {
          :joins => :tags,
          :conditions => ["tags.name = ?", tag]
      }
  }

  named_scope :is, lambda {
      |type| {
          :joins => :type,
          :conditions => ["types.name = ?", type]
      }
  }
这给了我像/conferences/in/salt_lake_city/with/joe_shmoe或/notices/about/programming/with/joe_splo/这样的URL,没有特定的顺序。赢