How to output text from embbeded ruby (in views)


#1

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.


#2

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) -%>


#3

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?


#4

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

Regards,

javier ramirez

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


#5

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


#6

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