The RoR equivalent of out.write() in JSP?


#1

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


#2

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


#3

I’m sure he knows what <%= expression %> does since he mentioned it
first :slight_smile:

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/


#4

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…


#5

Alex,

Thanks - that makes a lot of sense.

:slight_smile:

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…


#6

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


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


#7

Conditional expressions work:

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

Joe


#8

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.


#9

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

Wes

Dav Y. wrote:

I’m sure he knows what <%= expression %> does since he mentioned it
first :slight_smile:

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/