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

后端 未结 5 1730
误落风尘
误落风尘 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 17:09

    In the Rails Guides, it says:

    The params hash will always contain the :controller and :action keys, but you should use the methods controller_name and action_name instead to access these values

    ActionController Parameters

    So let's say you have a CSS class active , that should be inserted in any link whose page is currently open (maybe so that you can style differently) . If you have a static_pages controller with an about action, you can then highlight the link like so in your view:

  • About Us
提交回复
热议问题