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

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

Asked on December 28, 2018 in Ruby on Rails.
Add Comment


  • 3 Answer(s)

    There is a solution for controller in view:

    Controller_name holds the name of the controller used to serve  current view.

    Answered on December 28, 2018.
    Add Comment

    Note: The :controller and :action keys  use the methods controller_name and action_name instead to access  values.

    ActionController Parameters:

    CSS  active class  link full page is currently open that  can  different  style.static_pages controller with an about action, 

    <li>
      <a class='button <% if controller.controller_name == "static_pages" && controller.action_name == "about" %>active<%end%>' href="/about">
        About Us
      </a>
    </li>
    

    Answered on December 28, 2018.
    Add Comment

    Here alternatively try by using this below command: 

    #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) %>
    

    Answered on December 28, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.