Can I get the name of the current controller in the view?

后端 未结 5 1737
误落风尘
误落风尘 2020-11-29 16:17

Is there a way to figure out what the current controller is from within the view?

For an example of why I would want to know this: if several controllers share the s

5条回答
  •  囚心锁ツ
    2020-11-29 16:53

    #to get controller name:
    <%= controller.controller_name %>
    #=> 'users'
    
    #to get action name, it is the method:
    <%= controller.action_name %>
    #=> 'show'
    
    
    #to get id information:
    <%= ActionController::Routing::Routes.recognize_path(request.url)[:id] %>
    #=> '23'
    
    # or display nicely
    <%= debug Rails.application.routes.recognize_path(request.url) %>
    

    reference

提交回复
热议问题