我可以在视图中获取当前控制器的名称吗? [英] Can I get the name of the current controller in the view?

查看:96
本文介绍了我可以在视图中获取当前控制器的名称吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从视图内找出当前控制器?



有一个例子,为什么我想知道这个:如果几个控制器共享相同的布局,我可能有一个部分在布局ERB文件,我想突出显示当前页面的菜单项基于控制器。



也许这是一个坏方法。如果是这样,更好的方法是这样做?



我有兴趣知道如何获得当前控制器的名称。 >

(显然我可以在每个控制器中加入 @controller_name ='users'

解决方案

Rails会在后台执行,所以我只是想知道是否有一个内置的方法。 params [:controller] 保存用于投放当前视图的控制器的名称。


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 same layout, I may have a part in the layout ERB file where I want to highlight the current page's menu item based on the controller.

Maybe that is a bad approach. If so, what is the more preferred way to do this?

I'm interested to know about getting the name of the current controller either way, though.

(Obviously I could put something like @controller_name = 'users' in each controller; but that seems like the sort of thing Rails would've already done behind the scenes. So I'm just wondering if there's a built-in way.)

解决方案

params[:controller] holds the name of the controller used to serve the current view.

这篇关于我可以在视图中获取当前控制器的名称吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆