Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails access view name inside partial

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.

I was thinking about creating a page specific javascript file that would look up the div and and hide it.

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

like image 565
ShaggyInjun Avatar asked Jan 13 '12 05:01

ShaggyInjun


2 Answers

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)

like image 108
jhdavids8 Avatar answered Oct 05 '22 04:10

jhdavids8


you can get the name of the currently-rendering partial from within a Helper method with the following :

controller.view_context.view_renderer.instance_variable_get('@_partial_renderer').instance_values['path']
like image 45
Matt Storer Avatar answered Oct 05 '22 03:10

Matt Storer