Forum: Ruby on Rails <%= works, print doesn't

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.
D29980bb76cbac0f844c84dfcc52dadf?d=identicon&s=25 Ben Ch (bch36)
on 2005-12-27 23:29
In my views, for example list.rhtml, if I try:

<%= "test" %>

everything works fine.  If I try:

<% print "test" %>

then instead of outputting the page, the browser displays the HTML
source code.  Any ideas on what my problem is?  This is my first RoR app
so it's probably a stupid bug on my part, but my Google-fu isn't
powerful to track down an answer.
Ace7fa5337acbdf5897a6fc035897580?d=identicon&s=25 J. Ryan Sobol (Guest)
on 2005-12-27 23:34
(Received via mailing list)
Try this.

Hi, Mr. <% puts "Frodo" %>

~ ryan ~
D29980bb76cbac0f844c84dfcc52dadf?d=identicon&s=25 Ben Ch (bch36)
on 2005-12-27 23:41
Thanks for the quick reply!  Using "puts" keeps the browser from
displaying HTML code, but it doesn't output the text in the quotes.
I.e. the page displays properly, except the "puts" silently disappears.
Any other ideas?
Eb234d1ee9f1dcd334657d7c5c1b1c4d?d=identicon&s=25 Jamie Macey (jamie)
on 2005-12-28 00:01
(Received via mailing list)
On Tue, 2005-12-27 at 23:29 +0100, Ben CH wrote:
> so it's probably a stupid bug on my part, but my Google-fu isn't
> powerful to track down an answer.
>

The problem with this (and the other example using puts) is that for
your rails app, standard output is the console (check it out if you're
running Webrick for dev) - that's where your output is ending up.

I'm not sure if there's any way to get the output back via a command,
except perhaps <% return 'foo' %> - moral of the story is you probably
want to use <%=.  If you're building up a string to output, make that a
helper (a method in application_helper.rb) and have that method return
the correct output.  Then <%= helper_method %>

- Jamie
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2005-12-28 00:31
(Received via mailing list)
On Dec 27, 2005, at 2:41 PM, Ben CH wrote:

> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

You cant use print or puts inside of erb tags like that. It will mess
up the output just like you have encountered. So this:

<%= @foo %> is the way to print @foo to the page. There is now
workaround for this , you must not use print or puts in the erb
templates period.

Cheers-

-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
58c44a4a506d878f9a112f1d7b7cb87e?d=identicon&s=25 Jeremy Evans (Guest)
on 2005-12-28 00:40
(Received via mailing list)
On 12/27/05, Ben CH <eviloverlord@gmail.com> wrote:
> In my views, for example list.rhtml, if I try:
>
> <%= "test" %>
>
> everything works fine.  If I try:
>
> <% print "test" %>

This is by design.  You are supposed to use <%= x %>.  Using <% print
x %> will cause various problems depending on what STDOUT is.  Same
thing with <% puts x %>.  There is a Rails function named concat which
will do what you want.  <% concat x %> is the same as <%= x %>.
D29980bb76cbac0f844c84dfcc52dadf?d=identicon&s=25 Ben Ch (bch36)
on 2005-12-28 00:50
Jeremy Evans wrote:
> <% concat x %> is the same as <%= x %>.

Thanks for the response!  I gave it a try, but couldn't get it to work.
Doesn't the concat operation take at least two parameters by definition?
I can see creating a string using the concat operator, then using <%=
concatedString %> to print it out, but I can't think of any way that
simply doing <% concat x %> could work.  Any clarification?
132a94ca65959bda6c74fae54bff2425?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2005-12-28 01:16
(Received via mailing list)
On Dec 27, 2005, at 3:50 PM, Ben CH wrote:

>
You cant use print or puts inside of erb tags like that. It will mess
up the output just like you have encountered. So this:

<%= @foo %> is the way to print @foo to the page. There is now
workaround for this , you must not use print or puts in the erb
templates period.


Now if you want to use the concat  method you must do this:

<% _erbout.concat foo %>  OR <% _erbout << foo %>

_erbout is the stream that your erb template is compiling itself
into. So concatting to that or appending with << will do what you are
after. But <%= is much less to type and I find that if you think you
need to use the _erbout var then something is wrong and you might
want to think about refactoring your code.

Cheers-

-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
ezra@yakima-herald.com
A9f136bb1099c410158a6119c0633a40?d=identicon&s=25 Chris Lowder (Guest)
on 2005-12-28 01:26
(Received via mailing list)
If you took the time and consulted the rails API documentation on the
RoR website you would have found this very useful bit...

-------------------------------
concat(string, binding)
--------------------------------
The regular puts and print are outlawed in eRuby. It's recommended to
use the <%= "hello" %> form instead of print "hello". If you
absolutely must use a method-based output, you can use concat. It's
used like this: <% concat "hello", binding %>. Notice that it doesn't
have an equal sign in front. Using <%= concat "hello" %> would result
in a double hello.
58c44a4a506d878f9a112f1d7b7cb87e?d=identicon&s=25 Jeremy Evans (Guest)
on 2005-12-30 04:14
(Received via mailing list)
> > <% concat x %> is the same as <%= x %>.
>
> Thanks for the response!  I gave it a try, but couldn't get it to work.
> Doesn't the concat operation take at least two parameters by definition?
> I can see creating a string using the concat operator, then using <%=
> concatedString %> to print it out, but I can't think of any way that
> simply doing <% concat x %> could work.  Any clarification?

My mistake.  You'd need to use <% concat x, binding %>.  Note that the
api description states that "Using <%= concat "hello" %> would result
in a double hello.", which is false, since it would probably result in
an error.  I'm not sure why concat doesn't use the binding of it's
caller as a default if a binding is not specified explicitly,
presumably that's either an oversight or a technical limitation of the
ruby interpreter.
D29980bb76cbac0f844c84dfcc52dadf?d=identicon&s=25 Ben Ch (bch36)
on 2005-12-30 05:52
Jeremy Evans wrote:
> My mistake.  You'd need to use <% concat x, binding %>.

Thanks for the clarification.  I'm finding it hard to believe that there
is no simple way in Ruby to do <% print "true!" if something == true %>
on a webpage.  Am I just a bad programmer for having massive amounts of
logic on my webpages that print things conditionally?  I use the ternary
operator also, but that's so limiting as well.
3f900b38ec3b2c45427c354722fa4ce3?d=identicon&s=25 Tom Fakes (tomfakes)
on 2005-12-30 06:00
(Received via mailing list)
What's wrong with this:

	<%= "true!" if something == true %>
D29980bb76cbac0f844c84dfcc52dadf?d=identicon&s=25 Ben Ch (bch36)
on 2005-12-30 06:13
Tom Fakes wrote:
> What's wrong with this:
>
> 	<%= "true!" if something == true %>

Okay, I used a poor example, but this issue has gone a little beyond my
earlier problem, i.e. simply printing out something without having to
devote an entire ruby tag to it.  My problem is more along multiple
lines of code, e.g. a twenty line block of code with multiple print
statements in it.  I hate having to break up a block of code just to
print something out.

What I wish:
<%
print "X"
print "Y"
%>

What I get:
<%= "X" %>
<%= "Y" %>

Doesn't look so bad like that, but when you throw in a lot more logic
with the print statements sprinkled between them, having to break the
code into multiple <% %> blocks just to print something out is a little
frustrating.

Guess I'll have to go back to PHP. ;)  J/k, RoR is too sweet for that at
the moment, but a guy IS working on PhpOnTrax (phpOnTrax.org), which is
basically a PHP port of RoR.  (And it has a cooler acronym: POT)  It's
not entirely stable yet, but it's getting there.
3f900b38ec3b2c45427c354722fa4ce3?d=identicon&s=25 Tom Fakes (tomfakes)
on 2005-12-30 06:48
(Received via mailing list)
I don't know how much code you need to have in your templates, but one
of
the traps you can fall into is having too much code in them.  One of the
red
flags for this is needing large blocks of Ruby code.

RHTML files should be mostly HTML output, with a few places where you
drop
in your data.

If you do find you need to write a bunch of code to generate output,
then a
helper may be the better place for this code.  If you have program logic
in
there, then that should be part of the controller.

One of the things I liked about the Velocity template language was that
it
was *really* hard to do more than just output.  RHTML allows you to do a
bunch of cool Ruby things in the template file, but doing so is almost
always a bad idea.
This topic is locked and can not be replied to.