Forum: Ruby on Rails how to output something from within <% %> tags?

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.
39045f22049b1db3e3dd08c3bffac17e?d=identicon&s=25 Filip Godsmurf (godsmurf)
on 2006-02-23 17:29
simple question: how do I output something from within <% %> tags? e.g.
like 'echo' in PHP. I thought it would be 'puts' or 'print' but neither
seems to work. right now I always close the %> and open a <%= which is
tedious.
Aeb5810616aafc577c999d769d3f1bd2?d=identicon&s=25 Clint Pidlubny (Guest)
on 2006-02-23 17:39
(Received via mailing list)
You would use <% render :text => "whatever you want to write" %> but I'm
not
sure how that would be less tedious than <%= "whatever you want to
write" %>
B0d6fda6fabb8cda27b5e6dde2dc2ac3?d=identicon&s=25 Simo Addsw (gal)
on 2006-02-23 17:48
Filip Godsmurf wrote:
> simple question: how do I output something from within <% %> tags? e.g.
> like 'echo' in PHP. I thought it would be 'puts' or 'print' but neither
> seems to work. right now I always close the %> and open a <%= which is
> tedious.


From API docs:
  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.


Simo
 Addsw.it
39045f22049b1db3e3dd08c3bffac17e?d=identicon&s=25 Filip Godsmurf (godsmurf)
on 2006-02-23 18:01
Clint Pidlubny wrote:
> You would use <% render :text => "whatever you want to write" %> but I'm
> not
> sure how that would be less tedious than <%= "whatever you want to
> write" %>

Thanx but that doesn't work; render :text only works from within the
controller and it won't render anything else anymore. I just need a
command to echo stuff within a view.

With an echo command I could do this:

<%
  code
  echo "whatever you want to write"
  more code
%>

Whereas now I have to do this:

<%
  code
%>
<%= "whatever you want to write" %>
<%
  more code
%>

And that is very tedious.

An echo command is so basic, surely RoR has this?
39045f22049b1db3e3dd08c3bffac17e?d=identicon&s=25 Filip Godsmurf (godsmurf)
on 2006-02-23 18:15
Simo Gal wrote:
> From API docs:
>   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.

Thanx, that works but the ", binding" part makes it bad. I tried to put
this in application_helper.rb

  def echo(str)
    concat(str, binding)
  end

but that generates an error

  undefined local variable or method `_erbout' for
#<#<Class:0x384b968>:0x384b548>


Sometimes RoR annoys the hell out of me. Why would they deliberately
'outlaw' a most basic function?
22ff4abebc9bfd299524bb14449d95dc?d=identicon&s=25 Erik van Oosten (Guest)
on 2006-02-23 18:40
(Received via mailing list)
It is simple. Where would puts output to?
My guess is it would go to stdout, so to the webrick console?

     Erik.


Filip Godsmurf schreef:
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-23 22:13
(Received via mailing list)
On Feb 23, 2006, at 9:01 AM, Filip Godsmurf wrote:

>
> <%
>   code
> %>
> <%= "whatever you want to write" %>
> <%
>   more code
> %>
>
> And that is very tedious.
>
> An echo command is so basic, surely RoR has this?


Also instead of concat you can use _erbout if you dont like the
binding part. But I agree with others that its bad fomr to need this
much code in your views. This in not PHP so you dont use echo or
print. But if you must then you can use _erbout like this:

<%
code
_erbout << "Some text"
more code
%>

Cheers-
-Ezra
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-02-24 10:26
(Received via mailing list)
Filip Godsmurf wrote:
> #<#<Class:0x384b968>:0x384b548>
That's because if you put it in a helper method, the binding is picked
up from inside the helper method, which knows nothing about the
variables local to the erb script.  If you're going to be using that
technique, the binding() method actually has to be called in the erb.

>
> Sometimes RoR annoys the hell out of me. Why would they deliberately
> 'outlaw' a most basic function?
Because it's wrong to use it more often than it's right, maybe?
59bea58f406ca9f19f7820c79d61bfc9?d=identicon&s=25 Superbiji (Guest)
on 2006-02-24 10:53
(Received via mailing list)
On 2/24/06, Filip Godsmurf <krommenaas@gmail.com> wrote:
> With an echo command I could do this:
>   code
> %>
> <%= "whatever you want to write" %>
> <%
>   more code
> %>
>
> And that is very tedious.
>
> An echo command is so basic, surely RoR has this?

I also really hate this,
why there is no "echo" in rails

But I did this,

<%=
s = ""

code
s = s + "append something"
code

s
%>




--
Jual Beli Gratis egold 1$ http://shegold.com/
This topic is locked and can not be replied to.