rails 访问部分内的视图名称 [英] rails access view name inside partial
问题描述
我有一个在不同页面中使用的部分.我想根据呈现它的视图有条件地隐藏部分中的某个 div.
I have a partial that I am using in different pages. I want to conditionally hide a certain div inside the partial based on the view that is rendering it.
我正在考虑创建一个页面特定的 javascript 文件,该文件将查找 div 并将其隐藏.
I was thinking about creating a page specific javascript file that would look up the div and and hide it.
但是,如果有一种方法可以在局部检索视图名称/页面名称,它将成为局部的核心并且不需要在多个页面中加载相同的 javascript 文件.
But, if there was a way to retrieve the view name / page name inside the partial it would be central to the partial and would not necessitate loading the same javascript file in multiple pages.
有没有人知道如何在部分中做到这一点
Does anybody know a way to do this inside a partial
推荐答案
虽然@wahaj 的答案可行,但如果您想在一个中心位置做您需要的事情,您可以检查 controller_name
和action_name
部分中的变量来确定您所在的视图(例如 controller_name == "services"
和 action_name == "show"
让您知道您在服务控制器的显示视图中)
While @wahaj's answer would work, if you want to do what you need in a central location, you could check the controller_name
and action_name
variables in the partial to determine the view you're in (e.g. controller_name == "services"
and action_name == "show"
would let you know you're in the Show view for the Service controller)
这篇关于rails 访问部分内的视图名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!