There’s probably a simple answer to this simple problem, but I am trying
to figure out how to reference an instance variable within a layout. In
the controller, one method corresponds to a view of the same name. Those
instance variables in the method can be used in the corresponding view.
However, I want my instance variable to be accessible throughout all my
views, not just one, without having to set the instance variable in each
method. Any solutions? Thanks!
However, I want my instance variable to be accessible throughout all my
views, not just one, without having to set the instance variable in each
method. Any solutions? Thanks!
Would this work for you?
before_filter :init
def init @myvar = … #whatever you want here
end
init will run before any action is invoked, so I think it should be
available in your layout regardless of which action was called.
However, I want my instance variable to be accessible throughout all my
views, not just one, without having to set the instance variable in each
method. Any solutions? Thanks!
Would this work for you?
before_filter :init
def init @myvar = … #whatever you want here
end
init will run before any action is invoked, so I think it should be
available in your layout regardless of which action was called.
And if you want all controller to have an instance variable put that
snippet in application.rb
And layouts works just like views.
<%= @myvar %> will work identically in both layouts and views. And @myvar can be set in application.rb for all controllers in your whole
application via the above posters suggestion.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.