Ruby-on-rails 我可以在视图中获取当前控制器的名称吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3757491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I get the name of the current controller in the view?
提问by Dan Tao
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 已经在幕后做的事情。所以我只是想知道是否有内置方法。)
回答by Musili
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_nameand action_nameinstead to access these values
params 散列将始终包含 :controller 和 :action 键,但您应该使用方法controller_name和action_name来访问这些值
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_pagescontroller with an aboutaction, you can then highlight the link like so in your view:
因此,假设您有一个 CSS 类active,应该将其插入到页面当前打开的任何链接中(也许这样您可以设置不同的样式)。如果你有一个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>
回答by Anubhaw
controller_nameholds the name of the controller used to serve the current view.
controller_name保存用于服务当前视图的控制器的名称。
回答by Thaha kp
#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) %>
回答by Ricky Notaro-Garcia
controller_pathholds the path of the controller used to serve the current view. (ie: admin/settings).
controller_path保存用于为当前视图提供服务的控制器的路径。(即:)admin/settings。
and
和
controller_nameholds the name of the controller used to serve the current view. (ie: settings).
controller_name保存用于服务当前视图的控制器的名称。(即:)settings。
回答by Sami
If you want to use all stylesheet in your app just adds this line in application.html.erb. Insert it inside <head>tag
如果您想在您的应用程序中使用所有样式表,只需在 application.html.erb 中添加这一行。将其插入<head>标签内
<%= stylesheet_link_tag controller.controller_name , media: 'all', 'data-turbolinks-track': 'reload' %>
Also, to specify the same class CSS on a different controller
Add this line in the bodyof application.html.erb
另外,要在不同的控制器上指定相同的类 CSS 在application.html.erb 中
添加这一行body
<body class="<%= controller.controller_name %>-<%= controller.action_name %>">
So, now for example I would like to change the ptag in 'home' controller and 'index' action.
Inside index.scssfile adds.
因此,现在例如我想更改p“home”控制器和“index”操作中的标签。里面的index.scss文件添加。
.nameOfController-nameOfAction <tag> { }
.home-index p {
color:red !important;
}

