根据路由创建选择,如何?

时间:2010-06-02 09:04:12

标签: ruby-on-rails routing navigation

我正在尝试实现Tree Based Navigation中的导航,但是基于在routes.rb中定义的URL(命名路由,资源,...)。

是否可以检索routes.rb中定义的所有路径的集合?

所以我可以在这样的选择中使用它:

<%= f.collection_select :url, Route.all, :url, :name %>

TNX!

2 个答案:

答案 0 :(得分:1)

ActionController::Routing::Routes.routes

将列出应用程序的可用路由。将需要一些解析来提取适用的细节。

答案 1 :(得分:0)

Thanx到David Lyod的暗示我解决了它!

这是我的代码:

辅助-方法

# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper

  def routes_url
    routes = ActionController::Routing::Routes.routes.collect do |route|
      segs = route.segments.inject("") { |str,s| str << s.to_s }
      segs.chop! if segs.length > 1
      segs.chomp("(.:format)")
    end
    routes.delete_if {|x| x.index(':id')}
    return routes.compact.uniq.sort
  end
end

在我看来,我把:

<%= select("page", "url", options_for_select(routes_url), {:include_blank => true})  %>