# Trying to put ruby code in an if then statement in .rhtml

**URL:** <https://www.ruby-forum.com/t/trying-to-put-ruby-code-in-an-if-then-statement-in-rhtml/126378>\
**Category:** Rails\
**Created:** [January 17, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/trying-to-put-ruby-code-in-an-if-then-statement-in-rhtml/126378 "2008-01-17T21:04:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![railsnewb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@railsnewb](https://www.ruby-forum.com/u/railsnewb)\
**Post date:** [January 17, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/trying-to-put-ruby-code-in-an-if-then-statement-in-rhtml/126378/1 "2008-01-17T21:04:28Z")

</div>

This code, in my view(.rhtml file) causes the error  
which I’ll post below.

-----The Code-----  
\<%= if (@announcement.image == nil) then

Download

end %\>

----The Error----  
compile error  
./script/…/config/…/app/views/committee/audit\_archive.rhtml:118:  
syntax error  
\<a href = "\<%=  
url\_for\_file\_column(‘announcement’, ‘image’) ).to\_s);  
\_erbout.concat “" \>Download\n”

Any ideas on the cause of this? Thanks for any and  
all help!

```
  ____________________________________________________________________________________

```

Be a better friend, newshound, and  
know-it-all with Yahoo! Mobile. Try it now.  
[http://mobile.yahoo.com/;\_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ](http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ)

---

<div class="post-metadata">

**Author:** ![railsnewb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@railsnewb](https://www.ruby-forum.com/u/railsnewb)\
**Post date:** [January 17, 2008, 9:32pm UTC](https://www.ruby-forum.com/t/trying-to-put-ruby-code-in-an-if-then-statement-in-rhtml/126378/2 "2008-01-17T21:32:54Z")

</div>

Use \<% if… Not \<%= if…

---

<div class="post-metadata">

**Author:** ![railsnewb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@railsnewb](https://www.ruby-forum.com/u/railsnewb)\
**Post date:** [January 17, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/trying-to-put-ruby-code-in-an-if-then-statement-in-rhtml/126378/3 "2008-01-17T21:41:43Z")

</div>

\<% if condition %\>

\<% else %\>

\<% end %\>

Inside each part use normal ERB tags and HTML.

---

<div class="post-metadata">

**Author:** ![railsnewb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@railsnewb](https://www.ruby-forum.com/u/railsnewb)\
**Post date:** [January 17, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/trying-to-put-ruby-code-in-an-if-then-statement-in-rhtml/126378/4 "2008-01-17T21:44:05Z")

</div>

No luck!

The problem seems to be with the \<a href part of the  
code.

— Robert W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > \<%= if (@announcement.image == nil) then
> > 
> > Download
> > 
> > end %\>
> > 
> > ----The Error----  
> > compile error

./script/…/config/…/app/views/committee/audit\_archive.rhtml:118:

> >

* * *

> * * *
> 
> > Be a better friend, newshound, and  
> > know-it-all with Yahoo! Mobile. Try it now.

[http://mobile.yahoo.com/;\_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ](http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ)

> 

```
  ____________________________________________________________________________________

```

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
[http://tools.search.yahoo.com/newsearch/category.php?category=shopping](http://tools.search.yahoo.com/newsearch/category.php?category=shopping)

---

<div class="post-metadata">

**Author:** ![railsnewb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@railsnewb](https://www.ruby-forum.com/u/railsnewb)\
**Post date:** [January 17, 2008, 9:44pm UTC](https://www.ruby-forum.com/t/trying-to-put-ruby-code-in-an-if-then-statement-in-rhtml/126378/5 "2008-01-17T21:44:53Z")

</div>

> \<%= if (@announcement.image == nil) then

\<% if @announcement.image.nil? %\>  
…  
\<% end %\>

Ruby ain’t PHP 🙂

---

<div class="post-metadata">

**Author:** ![railsnewb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@railsnewb](https://www.ruby-forum.com/u/railsnewb)\
**Post date:** [January 17, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/trying-to-put-ruby-code-in-an-if-then-statement-in-rhtml/126378/6 "2008-01-17T21:50:34Z")

</div>

Both work, but for the sake of RUBY I probably should  
write it like this. Thanks.

— Sven S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

```
  ____________________________________________________________________________________

```

Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  
[http://tools.search.yahoo.com/newsearch/category.php?category=shopping](http://tools.search.yahoo.com/newsearch/category.php?category=shopping)

---

<div class="post-metadata">

**Author:** ![railsnewb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@railsnewb](https://www.ruby-forum.com/u/railsnewb)\
**Post date:** [January 17, 2008, 9:55pm UTC](https://www.ruby-forum.com/t/trying-to-put-ruby-code-in-an-if-then-statement-in-rhtml/126378/7 "2008-01-17T21:55:50Z")

</div>

Hi –

On Thu, 17 Jan 2008, jonathan Mcintire wrote:

> end %\>  
> See other replies for the right technique. One comment I’ll make is to  
> note in particular that ERb delimiters are not reentrant. To insert a  
> string conditionally, you could do something like this:

\<%= “\<a href=#{url\_for\_file\_column(‘announcement’,'image)}” if  
@announcement.image.nil %\>

and if the “if” fails, the whole expression will be nil, which will be  
interpolated as an empty string. It’s not generally the best way to do  
it, though; the separate \<% if %\> … \<% end %\> things are better,  
because they’re easier to parse visually and also they don’t  
interpolate anything, not even an empty string. Also, the if-modifier  
is fragile; it won’t be happy if you break the line before it. I would  
mainly use the above technique for short things like:

\<%= @user.middle\_name %\>

where I can go ahead and do the interpolation without really caring  
whether the value is nil or not (or “” for that matter). Except what  
I’d really do is write a User#whole\_name method 🙂 But you see the  
point.

David

–  
Training for 2008!  
Ruby on Rails training by David A. Black/Ruby Power and Light, LLC:  
\* Intro to Rails, New York, NY, February 4-7 2008  
\* Advancing With Rails, New York, NY, February 11-14 2008  
Hosted by Exceed Education. See [http://www.rubypal.com](http://www.rubypal.com) for details!

---

<div class="post-metadata">

**Author:** ![railsnewb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@railsnewb](https://www.ruby-forum.com/u/railsnewb)\
**Post date:** [January 17, 2008, 10:02pm UTC](https://www.ruby-forum.com/t/trying-to-put-ruby-code-in-an-if-then-statement-in-rhtml/126378/8 "2008-01-17T22:02:57Z")

</div>

This did it!  
Thanks for responding everyone.

— Jeremy Weiskotten  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

```
  ____________________________________________________________________________________

```

Be a better friend, newshound, and  
know-it-all with Yahoo! Mobile. Try it now.  
[http://mobile.yahoo.com/;\_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ](http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ)

---

<div class="post-metadata">

**Author:** ![railsnewb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f07891/32.png) [@railsnewb](https://www.ruby-forum.com/u/railsnewb)\
**Post date:** [January 17, 2008, 9:54pm UTC](https://www.ruby-forum.com/t/trying-to-put-ruby-code-in-an-if-then-statement-in-rhtml/126378/9 "2008-01-17T21:54:59Z")

</div>

Try this…

\<% if @announcement.image.nil? %\>  
\<%= link\_to ‘Download’, url\_for\_file\_column(‘announcement’, ‘image’) %\>  
\<% end %\>
