Forum: Ruby on Rails how to output text from embbeded ruby (in views)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
6fe135329ce89e00b74839a41c8e3872?d=identicon&s=25 Ehud Rosenberg (ehudros)
on 2007-04-26 12:40
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.
09e6ba06911a5781c04be8554f114aba?d=identicon&s=25 Rob Nichols (reggieb)
on 2007-04-26 13:04
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) -%>
6fe135329ce89e00b74839a41c8e3872?d=identicon&s=25 Ehud Rosenberg (ehudros)
on 2007-04-26 13:17
Rob Nichols 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?
1f2eadfb41362800ebc2cf211b91d0f7?d=identicon&s=25 javier ramirez (Guest)
on 2007-04-26 19:02
(Received via mailing list)
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 !!Es gratis!!
3c458017e52f646945b899aa5d380a0f?d=identicon&s=25 Matthew Isleb (misleb)
on 2007-04-26 19:12
Ehud Rosenberg wrote:
> Rob Nichols 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
09e6ba06911a5781c04be8554f114aba?d=identicon&s=25 Rob Nichols (reggieb)
on 2007-04-27 10:25
Matthew Isleb wrote:
> Ehud Rosenberg 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:

<p><%= "string" %></p>

outputs:

<p>string
</p>

Whereas

<p><%= "string" -%></p>

outputs:

<p>string</p>
This topic is locked and can not be replied to.