How can I avoid nil object errors if a var isn’t set?
<% if @var %><%=h @var %><% end %>
Also, is there a way to reduce the number of code
delimiters, something like:
<% if @var print @var %>
Thanks!
CSN
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Nov 7, 2005, at 5:58 PM, CSN wrote:
Ah!
<% if nil ^ @var %><%=h @var %><% end %>
I’m still interested in knowing if there’s a way to
reduce the number of <% and %>'s!
Since
nil.to_s == ‘’
and
h(nil) == ‘’
just do
<%=h @var %>
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)
iD8DBQFDcAeCAQHALep9HFYRAubCAKC/UnfAnCz8ZRbEFt+sbPg3gUBNcwCgiBlG
uQTsZLuGgWoLIttk95/EPyk=
=KmyP
-----END PGP SIGNATURE-----
Ah!
<% if nil ^ @var %><%=h @var %><% end %>
I’m still interested in knowing if there’s a way to
reduce the number of <% and %>'s!
Thanks,
CSN
— CSN [email protected] wrote:
Thanks!
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
Yahoo! FareChase: Search multiple travel sites in one click.
http://farechase.yahoo.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Nov 7, 2005, at 6:18 PM, CSN wrote:
Ah, easy enough. Is there a way to simplify this code?
<% if nil ^ @var %><%=h @var %>, <% end %><%=h @var2
%>
If @var isn’t empty, I want a comma and space to
follow it. Otherwise just output @var2 . Coming from
PHP’s Smarty, I used:
{if $var}{$var}, {/if}{$var2}
Not sure it’s simplified, but it’s shorter:
<%= "#{h(@var )}, " if @var %><%=h @var2 %>
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)
iD8DBQFDcA3/AQHALep9HFYRAkCOAKDCzNcJ6UvmAhEoZXC3z9vyK9gmFwCeJ9lu
9ORPPphAaBNNt/TrnwfHBw4=
=K7r3
-----END PGP SIGNATURE-----
Ah, easy enough. Is there a way to simplify this code?
<% if nil ^ @var %><%=h @var %>, <% end %><%=h @var2
%>
If @var isn’t empty, I want a comma and space to
follow it. Otherwise just output @var2 . Coming from
PHP’s Smarty, I used:
{if $var}{$var}, {/if}{$var2}
Thanks
CSN
— Jeremy K. [email protected] wrote:
to
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)
iD8DBQFDcAeCAQHALep9HFYRAubCAKC/UnfAnCz8ZRbEFt+sbPg3gUBNcwCgiBlG
uQTsZLuGgWoLIttk95/EPyk=
=KmyP
-----END PGP SIGNATURE-----
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
Start your day with Yahoo! - Make it your home page!
http://www.yahoo.com/r/hs
— Jeremy K. [email protected] wrote:
to
reduce the number of <% and %>'s!
Since
nil.to_s == ‘’
and
h(nil) == ‘’
just do
<%=h @var %>
That doesn’t work for this though:
<%=h @var.name %>
It gives a nil object error.
CSN
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)
iD8DBQFDcAeCAQHALep9HFYRAubCAKC/UnfAnCz8ZRbEFt+sbPg3gUBNcwCgiBlG
uQTsZLuGgWoLIttk95/EPyk=
=KmyP
-----END PGP SIGNATURE-----
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
Yahoo! Mail - PC Magazine Editors’ Choice 2005
[object
Not sure it’s simplified, but it’s shorter:
<%= "#{h(@var )}, " if @var %><%=h @var2 %>
Or maybe
<%=h [var, var2].compact.join(",") %>
especially useful if you have more than one variable.
Hadley
Nice. This does exactly what I want:
<%= h("#{@var.name }, ") if @var %>
Thanks everybody!
CSN
— Ronny H. [email protected] wrote:
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
Yahoo! Mail - PC Magazine Editors’ Choice 2005
[object
That doesn’t work for this though:
<%=h @var.name %>
It gives a nil object error.
CSN
What I have used is
<%= h @var.name if !@var.nil ? %>
Warmest regards,
Nathan.
On Mon, Nov 07, 2005 at 06:45:19PM -0800, CSN wrote:
<%=h @var.name %>
It gives a nil object error.
How about this?
<%= h(@var.name ) if @var %>
Ronny
What I have used is
<%= h @var.name if !@var.nil ? %>
<%= h @var.name unless @var.nil ? %>
On Nov 7, 2005, at 5:51 PM, CSN wrote:
CSN
Try this:
<%= @var rescue nil %>
HTH-
-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732
On Nov 7, 2005, at 5:51 PM, CSN wrote:
CSN
-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732
On Nov 7, 2005, at 11:21 PM, Ezra Z. wrote:
<% if @var print @var %>
Thanks!
CSN
Try this:
<%= @var rescue nil %>
If @var has not been set, it will evaluate to nil, which when to_s’ed
(as I think Jeremy
pointed out) will be ‘’, so this construct is not useful (as it will
not throw an exception
to be rescued).
Just doing
<%= @var %>
will show you what’s in @var or ‘’ if @var has been set (or is
otherwise nil). To address
further up in the thread, you are not getting nil object errors from
this construct,
rather from something like <%= @foo.bar %>, and I think that has been
addressed
earlier with constructs like <%= @foo.bar if @foo %> and so on.