Created
November 7, 2011 02:28
-
-
Save kinopyo/1344066 to your computer and use it in GitHub Desktop.
Passing local optional variables to sub template views.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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