HOWTO: Call a nil :local variable from a partial
Posted by Sandro Paganotti in
Ruby on Rails -
comments are closed
Have you notice that if you try to execute a partial like the following:
# _my_name.rhtml
This is a partial and this is my name: <%=name%>
without calling it with a :name locale you are prompted this nice Exception ?
undefined local variable or method `name' for #<#<Class:0x542ae28>:0x542ad24>
The only way to avoid this exception is to execute a line of code that checks if the variable is nil and, in case of positive answer, it set that variable to nil! I know this could sound quite weird but it’s the only way I found to make thing work.
So, here is the line:
<%
# _my_name.rhtml
name = name.nil? ? false : name
%>
If someone know why this strange behavior happens please let me know.


Comments
marcus derencius
Posted on February 04
Nikos D.
Posted on February 05
Tarmo T.
Posted on February 07
Sunny
Posted on February 20