How can I output/render text within a view, but within a block of ruby
code
that is surrounded by <% %> ?
I know that use of <%= %> works, but if I have several lines of ruby in
the
view that is within <% %> and I want to output text to be part of the
HTML
how can I do this?
For example something like “puts” or “render” but actually works within
a
*.rthml view.
While this is the way to do it, I would suggest writing multi-line ruby
code as a helper and to return the HTML-Code. Something like this:
–
module YourControllerHelper
def my_output(model)
name = strip_tags(model.name)
description = strip_tags(model.description)
html = <<HTML
#{name}
#{description}
HTML
end
end
View:
<%= my_output %>
Another version is to substitute the here_doc with a render-call using a
partial.
There are multiple versions to accomplish this, but the above is the
quickest to write ;).
Remco Van 't veer wrote:
The following:
<% _erbout += Time.now %>
and
<% concat Time.now, binding %>
are the same as:
<%= Time.now %>
Greg H. wrote:
How can I output/render text within a view, but within a block of ruby
code that is surrounded by <% %> ?
Stylistically, though, you should only use the concat if it’s truly
unavoidable. In general, you shouldn’t be putting too much Ruby code
into your views. As Florian suggests, think about pushing this out to
a helper so that you can use the clean, readable <%= %> tags.
I’m basically just curious to understand whether it was possible to
output
from within <% %>, to relate possible use within rails views with Java
JSP. The answer seems to be that it is not possible, which is fine, I
just
wanted to understand.
So you have to do:
<% @foo.each do |bar| %>
<%= bar %>
<% end %>
As there is no way of doing something like:
<% @foo.each do |bar|
render_to_view bar
end
%>
ok thanks Lionel - I’ve understood the concat method too. Seems like
concat
would be the slightly less “under the bonnet” approach to use. Neither
seemed to get mentioned in the “Agile Web D. with Rails” book
in
this context which is interesting.