I'm using the Rails 5 ApplicationController.renderer.render
method to render from within a model. I need to pass some variables to my layout which I have done using the locals
option; this variable is then available in the layout if accessed directly, but not via self
.
Here is how I have setup my render
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 within the layout I want to do something like this.
<div id="foo" class="<%= self.action_name %>">
I was able to get this working by dropping self
in this particular instance
<div id="foo" class="<%= action_name %>">
but now my concern is how would I set a variable so that it would work correctly with self
? Previously I was using the render_anywhere gem and this was handled using rendering_controller.var = "value"
Since self
is a keyword in Ruby, you cannot use it as a method call in your layout template, so you should use another 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 a @, you should put it inside a string '@my_object': self
and calls it normally in your template: <%= @my_object.action_name%>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With