how to output text from embbeded ruby (in views)


<%= @user.nil? "not logged in" :>

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" :>

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 :wink:

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.


javier ramirez