3

Is there a way to look up the HTML for a given controller action? For example, I would like to be able to associate GET with index and PUT with update. I want to be able to do this dynamically based on the routes.

I can get the action methods for each controller using Controller.action_methods, but this returns a set of strings of action methods. Ideally what I would like is a hash of the form: {:action => :verb}.

3 Answers 3

2

Read the rake routes task, that will provide insight:

e.g:

users GET    /users(.:format)     {:controller=>"users", :action=>"index"}

I assume this is what you are after?

Sign up to request clarification or add additional context in comments.

3 Comments

That is the data I'm looking for indeed, but now how I want to get it. I'm trying to generate simple test cases for all of my actions which require user authentication. See my previous question: stackoverflow.com/questions/1137589/… . I'm trying to query the Controller for it's actions and find what verb they are associated to produce the actions variable in that example.
/Library/Ruby/Gems/1.8/gems/rails-2.3.2/lib/tasks/routes.rake
@BryanWard I had the same problem as you - finally a real answer below (only two years later // using rails 3.2+)
0

:method is part of a :conditions hash you can pass in to map.connect

  map.connect 'post/:id', :controller => 'posts', :action => 'create_comment',
              :conditions => { :method => :get }

1 Comment

I'm looking to essentially query the map to find the method for an action. So in your example I would like to be able to start with "create_comment" and have a way to find :post.
0

To provide a useful object with all controllers, actions, and associated verbs

def all_routes
    @all_routes ||= Rails.application.routes.routes.map do |route|
          { alias: route.name,
            path: route.path.spec.to_s,
            controller: route.defaults[:controller],
            action: route.defaults[:action],
            verb: route.verb.source.to_s.delete('$'+'^')
          }
    end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.