我可以在视图中获取当前控制器的名称吗?

时间:2022-10-24 07:32:04

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.

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

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.)

(显然,我可以在每个控制器中放置类似于@controller_name = 'users'的内容;但这似乎是Rails在幕后已经做过的事情。所以我想知道是否有内置的方法。

3 个解决方案

#1


195  

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

controller_name保存为当前视图服务的控制器的名称。

#2


244  

Also, in the Rails Guides, it says:

同样,在Rails指南中,它说:

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

params散列将始终包含:controller和:action键,但是应该使用controller_name和action_name方法来访问这些值

ActionController Parameters

ActionController参数

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:

假设您有一个CSS类活动,它应该被插入到任何页面当前打开的链接中(也许这样您就可以使用不同的样式了)。如果你有一个static_pages控制器和一个about动作,你可以在你的视图中这样突出显示链接:

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

#3


80  

#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'

reference

参考

#1


195  

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

controller_name保存为当前视图服务的控制器的名称。

#2


244  

Also, in the Rails Guides, it says:

同样,在Rails指南中,它说:

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

params散列将始终包含:controller和:action键,但是应该使用controller_name和action_name方法来访问这些值

ActionController Parameters

ActionController参数

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:

假设您有一个CSS类活动,它应该被插入到任何页面当前打开的链接中(也许这样您就可以使用不同的样式了)。如果你有一个static_pages控制器和一个about动作,你可以在你的视图中这样突出显示链接:

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

#3


80  

#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'

reference

参考