# How to output text from embbeded ruby (in views)

**URL:** <https://www.ruby-forum.com/t/how-to-output-text-from-embbeded-ruby-in-views/92996>\
**Category:** Rails\
**Created:** [April 26, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/how-to-output-text-from-embbeded-ruby-in-views/92996 "2007-04-26T12:40:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 26, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/how-to-output-text-from-embbeded-ruby-in-views/92996/1 "2007-04-26T12:40:24Z")

</div>

This is a pretty simple question I can’t find the answer to…  
Obviously \<%= %\> directs the output of all the commands inside the block  
to the html file being generated.  
that works fine with rails helpers and variables, but what if I simply  
want to output a string if a certain condition was met?

e.g:  
\<%= @user.nil? “not logged in” : @user.name%\>

so if the user is not logged an appropraite text is echoed.  
I’ve tried print (which ends up doing strange things) and puts (which  
gives me a 500 error no less).

How is this done?

Thanks,  
Ehud

PS - I know I can break this up so the text would no be inside the erb  
code, but that ends up with some really ugly code.

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 26, 2007, 1:04pm UTC](https://www.ruby-forum.com/t/how-to-output-text-from-embbeded-ruby-in-views/92996/2 "2007-04-26T13:04:08Z")

</div>

This should do it:

\<%= “non logged in” unless @user -%\>

Or you could create a helper method in application\_helper:

def log\_on\_status(user=nil)  
if user  
return “Logged on as #{user.name}”  
else  
return “User not logged on”  
end  
end

And then in the view enter

\<%= log\_on\_status(@user) -%\>

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 26, 2007, 1:17pm UTC](https://www.ruby-forum.com/t/how-to-output-text-from-embbeded-ruby-in-views/92996/3 "2007-04-26T13:17:09Z")

</div>

Rob N. wrote:

> This should do it:
> 
> \<%= “non logged in” unless @user -%\>
> 
> Or you could create a helper method in application\_helper:
> 
> def log\_on\_status(user=nil)  
> if user  
> return “Logged on as #{user.name}”  
> else  
> return “User not logged on”  
> end  
> end
> 
> And then in the view enter
> 
> \<%= log\_on\_status(@user) -%\>

the “-” at the end ( -%\> ) makes this possible?  
Is there no way to just echo a string to the screen?

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 26, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/how-to-output-text-from-embbeded-ruby-in-views/92996/4 "2007-04-26T19:02:11Z")

</div>

Hi,

> \<%= @user.nil? “not logged in” : @user.name%\>
> 
> so if the user is not logged an appropraite text is echoed.  
> I’ve tried print (which ends up doing strange things) and puts (which  
> gives me a 500 error no less). How is this done?

your approach is good, but you got misleaded by the question mark.

nil? is a method of object, so the whole name of the method is nil?,  
with the question mark included.

if you want to use the operator ? the syntax is

```
condition ? result_if_true : result_if_false

```

the trick here is that in your case the condition would be @user.nil?  
and then the syntax would be

\<%= @user.nil? ? “not logged in” : @user.name%\>

notice the extra ? you didn’t have before. So… you were really close.  
Because of ruby accepting ? as a method name i had a hard time at the  
beginning using the ? operator coming from other languages. Finally I  
got used to use it always like

(condition) ? (if\_true) : (if\_false)

this way, by using the brackets, it makes easier to have the right  
syntax at the first try 😉

And, anyway, to complete the answer to your question… for really  
really really special cases where you cannot just use \<%=%\> syntax for  
some obscure reason (usually complex helpers with blocks), you could use  
the concat method. As documentation says, the \<%=%\> standard erb syntax  
is preferred.

Regards,

javier ramirez

## –

Estamos de estreno… si necesitas llevar el control de tus gastos  
visita [http://www.gastosgem.com](http://www.gastosgem.com) !!Es gratis!!

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 27, 2007, 10:25am UTC](https://www.ruby-forum.com/t/how-to-output-text-from-embbeded-ruby-in-views/92996/5 "2007-04-27T10:25:39Z")

</div>

Matthew I. wrote:

> Ehud R. wrote:

> > the “-” at the end ( -%\> ) makes this possible?  
> > Is there no way to just echo a string to the screen?
> 
> The “-” is just supressing a newline in the HTML output. It has no  
> affect on the logic.
> 
> This echoes a string:
> 
> \<%= “string” %\>

The use of -%\> is a style thing. I tend to use it situations like this  
where just a small bit of text is being dynamically inserted. I use it  
because I think the resulting HTML is neater, easier to read and  
therefore easier to debug. If you don’t use it a newline gets added  
after each dynamic entry. For example:

\<%= "string" %\>

outputs:

string

Whereas

\<%= "string" -%\>

outputs:

string

---

<div class="post-metadata">

**Author:** ![ehudros](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/3ec8ea/32.png) [@ehudros](https://www.ruby-forum.com/u/ehudros)\
**Post date:** [April 26, 2007, 7:12pm UTC](https://www.ruby-forum.com/t/how-to-output-text-from-embbeded-ruby-in-views/92996/6 "2007-04-26T19:12:19Z")

</div>

Ehud R. wrote:

> Rob N. wrote:
> 
> > This should do it:
> > 
> > \<%= “non logged in” unless @user -%\>
> > 
> > Or you could create a helper method in application\_helper:
> > 
> > def log\_on\_status(user=nil)  
> > if user  
> > return “Logged on as #{user.name}”  
> > else  
> > return “User not logged on”  
> > end  
> > end
> > 
> > And then in the view enter
> > 
> > \<%= log\_on\_status(@user) -%\>
> 
> the “-” at the end ( -%\> ) makes this possible?  
> Is there no way to just echo a string to the screen?

The “-” is just supressing a newline in the HTML output. It has no  
affect on the logic.

This echoes a string:

\<%= “string” %\>

(thought it isn’t very useful, obviously because it is just a static  
string)

This conditionally echoes a string:

\<%= “string” if @user %\>

-matthew
