Rails, Passing a variable from the controller to the App Layout
I have the following:
In my controller:
def gettingstarted
@title = "Getting Started"
respond_to do |format|
format.html { render :layout => true, :showLeftNav => false }
end
end
In my default application layout file Layouts/application.html.erb:
<% showLeftNav = true if local_assigns[:showLeftNav].nil? %>
<body style="<% if showLeftNav == false %>开发者_如何学Python;background:none !important;<% end %>">
Am I passing showLeftNav incorrectly from the controller to the layout?
Why not define this as an instance variable like the @title
? That way you could just for the presence of it in the view and also default its value.
local_assigns will be used in the templates (ActionView) to pass local variables to sub templates or a partial. For passing values from controller to view we will be using instance variables. This page has an example showing how to pass local variables to sub templates and this shows some Public Instance methods where local variables can be passed.
精彩评论