Create select based on routing, how?
I am trying to implement navigation like in Tree Based Navigation but based on U开发者_如何学编程RLs defined in routes.rb (named routes, resources, ...).
Is it possible to retreive a collection of all routes defined in routes.rb?
So I can use it in a select like this:
<%= f.collection_select :url, Route.all, :url, :name %>
Tnx!
ActionController::Routing::Routes.routes
Will list available routes for the application. Will require some parsing to pull out applicable details.
Thanx to the hint of David Lyod I solved it!
Here is my code:
helper-method
# 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
and in my view I put:
<%= select("page", "url", options_for_select(routes_url), {:include_blank => true}) %>
精彩评论