Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kinopyo/1344066 to your computer and use it in GitHub Desktop.
Save kinopyo/1344066 to your computer and use it in GitHub Desktop.
Passing local optional variables to sub template views.
# this will work
<% unless defined?(:show_author) %>
<% show_author = true %>
<% end %>
# NOT work
<% show_author = true unless defined?(:show_author) %>
# this will work
<% show_author = true unless local_assigns.has_key? :show_author %>
# http://api.rubyonrails.org/classes/ActionView/Base.html
# If you need to find out whether a certain local variable
# has been assigned a value in a particular render call,
# you need to use the following pattern:
<% unless local_assigns.has_key? :show_author %>
<% show_author = true %>
<% end %>
<% if show_author %>
Author: <%= @article.author %>
<% end %>
# Testing using defined? headline will not work. This is an implementation restriction.
<%= render 'article', :show_author => false %>
<%= render 'article' %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment