Rails 5 rendering from outside the controller with ApplicationController.renderer.render does not set variables on itself

I am using the Rails 5 ApplicationController.renderer.render method to render inside the model. I need to pass some variables to my layout, which I did with the locals option; this variable is then available in the layout if it can be obtained directly, but not through self .

This is how I set up the rendering

 html_string = ApplicationController.renderer.render( file: "/#{template_path}/base/show", :formats => [:pdf,:html], locals: { :@routing_form => self, :controller_name => controller_name, :action_name => action_name, :current_user => current_user }, :layout => '/layouts/application' ) 

Then in the layout I want to do something like this.

 <div id="foo" class="<%= self.action_name %>"> 

I managed to get this working by dropping self on this particular instance

 <div id="foo" class="<%= action_name %>"> 

but now I'm worried about how to set a variable so that it works correctly with self ? I used to use the render_anywhere gem, and this was handled using rendering_controller.var = "value"

+6
source share
1 answer

Since self is a keyword in Ruby, you cannot use it as a method call in a layout template, so you must use a different name to pass with the locals.

You can pass something like my_object: self , and it will work fine.

If you want to name the key with @, you should put it inside the line '@my_object': self and usually call it in your template: <%= @my_object.action_name%>

+3
source

All Articles