how get all routes in my rails application?

后端 未结 3 1832
情书的邮戳
情书的邮戳 2020-12-25 14:09

can I get all routes in my rails application? I need an output like rake routes and put the result in an array.

Is it possible? how?

相关标签:
3条回答
  • 2020-12-25 14:29

    You could have a look at way rails spits out those routes from the rake task. It's in /gems/rails/2.3.x/lib/tasks/routes.rake for Rails 2. Seems to be basically doing ActionController::Routing::Routes.routes in the general case and then interrogating that.

    0 讨论(0)
  • 2020-12-25 14:32

    Well, independently of where you need it, you could do:

    routes = `rake routes`.split("\n")
    

    Or even:

    routes = `rake routes`.split("\n").map{ |r| r.gsub(', ', ',').split(' ') }
    
    0 讨论(0)
  • 2020-12-25 14:32

    In order to spread the headache that it was to acomplish that, i did this based on @jordini answer:

     Rails.application.routes.routes.to_a.each do |rota|
       if rota.app.instance_variable_defined? '@defaults'
          perm = Permission.where({
            :acao => rota.defaults[:action],
            :controller => rota.defaults[:controller], 
            :verbo => rota.verb.source.to_s.gsub("^","").gsub("$","").downcase
          ).first_or_create
        end
     end
    

    This was what i've used to create all permissions for my web page at once.

    0 讨论(0)
提交回复
热议问题