Alex Rivera | Logout

Rails 3.1.4 - Render :text

Asked 2012-03-30T00:51:20.117
16

I'm updating my rails 2 apps to rails 3 and find that the use of 'render :text' does not behave the same anymore.

@results is an array. In my controller:

render :text => "<ul>#{@results}</ul>"

It's returning the whole array as a string rather than iterating through each value:

<ul>
  ["
  <li>Steve</li>
  ", "
  <li>John</li>
  "]
</ul>

Worked fine in Rails 2.x but not in 3. How do I fix this?

I'm expecting a result of:

<ul>
  <li>Steve</li>
  <li>John</li>
</ul>
Edit
Report

1 Answer

2

I would suggest doing the following instead of render :text

render :partial => "result", :collection => @results

and add the file: _result.html.erb with

<ul>
  <%= result %>
</ul>

or even better if you can remove the li tags from @results

<ul>
  <li><%= result %></li>
</ul>

The Rails 3 docs say render text should be used for NON HTML text, which does not fit your use case. Using render :partial :collection is a better and more rails 3 way to iterate through your list.

answered 2012-03-30T02:36:17.833

Your Answer