Forum: Ruby on Rails The RoR equivalent of out.write() in JSP?

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.
Wes G. (Guest)
on 2006-03-14 22:09
All,

In JSP, I can output strings in the Web page by either

<%= foo %> //foo is a string or returns a string
or
<% out.write("test") %> //write directly to the output stream.

What is the method of "writing to the output stream" in RoR?  Basically,
what is the equivalent of out.write()?

I have an if then statement that I want to put around a call to

h some_stuff

Thanks,
Wes
Pat M. (Guest)
on 2006-03-14 22:21
(Received via mailing list)
Not entirely sure what out.write() does (just outputs to the page, I
guess?).  If you want to conditionally display something though, do

<%= h some_stuff if some_cond? %>

If it's a bit more complex you can do
<% if some_cond? %>
<%= h some_stuff %>
<%= h some_other_stuff %>
<% end %>

<%= expression %> writes the result of expression to the page.

Pat
Dav Y. (Guest)
on 2006-03-14 22:51
(Received via mailing list)
I'm sure he knows what <%= expression %> does since he mentioned it
first :)

I don't have an answer Wes, but I've wished for the same thing.
Sometimes it is just aesthetically nicer to use something like
out.write() in a <% %> code block. Perhaps it affects people who use
color coded editors more...

On 3/14/06, Pat M. <removed_email_address@domain.invalid> wrote:
>
> >
> >
> >
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


--
Dav Y.
http://AkuAku.org/
Wes G. (Guest)
on 2006-03-14 23:02
Yeah, it just seems so cheesy to me to do

<% if %>
<%= stuff %>
<% else %>
<%= other stuff %>
<% end %>

especially since we _know_ there _must_ be some kind of handle to the
output stream anyway.

Actually, maybe not at the Ruby level - I don't understand the IO model
for Ruby yet, so I can't say.  But it seems like there should be some
suitable abstraction, no?

Anyhow, cheese is ok for now :)

Wes


Dav Y. wrote:
> I'm sure he knows what <%= expression %> does since he mentioned it
> first :)
>
> I don't have an answer Wes, but I've wished for the same thing.
> Sometimes it is just aesthetically nicer to use something like
> out.write() in a <% %> code block. Perhaps it affects people who use
> color coded editors more...
>
> On 3/14/06, Pat M. <removed_email_address@domain.invalid> wrote:
>>
>> >
>> >
>> >
>> _______________________________________________
>> Rails mailing list
>> removed_email_address@domain.invalid
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>>
>
>
> --
> Dav Y.
> http://AkuAku.org/
Alex Y. (Guest)
on 2006-03-14 23:12
(Received via mailing list)
Wes G. wrote:
> Yeah, it just seems so cheesy to me to do
>
> <% if %>
> <%= stuff %>
> <% else %>
> <%= other stuff %>
> <% end %>
>
Try:

<%= if some_condition
         some_code_evaluating_to_a_string
     else
         some_other_string
     end %>

That works because if actually returns a value...
Wes G. (Guest)
on 2006-03-14 23:30
Alex,

Thanks - that makes a lot of sense.

:)

Wes

Alex Y. wrote:
> Wes G. wrote:
>> Yeah, it just seems so cheesy to me to do
>>
>> <% if %>
>> <%= stuff %>
>> <% else %>
>> <%= other stuff %>
>> <% end %>
>>
> Try:
>
> <%= if some_condition
>          some_code_evaluating_to_a_string
>      else
>          some_other_string
>      end %>
>
> That works because if actually returns a value...
Ezra Z. (Guest)
on 2006-03-14 23:56
(Received via mailing list)
On Mar 14, 2006, at 12:09 PM, Wes G. wrote:

> what is the equivalent of out.write()?
you can use two ways to do this:

<% _erbout << "foo" %>
OR
<% concat("foo", binding) %>

Neither of which is very pretty ;)


>
> _______________________________________________
> Rails mailing list
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

-Ezra Z.
Yakima Herald-Republic
WebMaster
http://yakimaherald.com
509-577-7732
removed_email_address@domain.invalid
Joe (Guest)
on 2006-03-15 00:02
Conditional expressions work:

<%= (1==1) ? 'yep' : 'nope' %>
<%= (1==2) ? 'yep' : 'nope' %>


Joe
Jay L. (Guest)
on 2006-03-15 03:21
(Received via mailing list)
On Tue, 14 Mar 2006 21:09:36 +0100, Wes G. wrote:

> What is the method of "writing to the output stream" in RoR?  Basically,
> what is the equivalent of out.write()?
>
> I have an if then statement that I want to put around a call to

Usually, when I've wanted to do that, I realized that the view was
actually
getting too complicated, and the code really belonged in either the
model
or a helper... I'm not saying that's always the case, but it's something
to
consider.

Jay L.
This topic is locked and can not be replied to.