views:

1993

answers:

2

I'm probably missing something obvious here but here's what I'm trying to do.

From the view, I'm calling a custom helper function

<div>
  <%=display_services%>
</div>

In the helper file with the display_services function

def display_services
  html = "<div>"
  form_for @user do |f|
   f.text_field ...
  end
 html << "</div>"
end

I find that form_for method and f.text_field output directly to HTML stream without the div wrapper that I like. What is the proper syntax to output all the HTML in display_services? Thanks in advance for your help.

+2  A: 

As it turns out, I had to do something like this

def display_services
  html = "<div>"
  html << (form_for @user do |f|
   f.text_field ...
  end)
  html << "</div>"
end

Note the () wrapped around the form block. If someone has a better solution, let me know.

Bob
+6  A: 

Just a suggestion for style, I like doing something like this:

In your view:

<% display_services %>

Please note that the = isn't needed any more. The helper then uses concat() to append something to your page and the putting-long-strings-together thing is obsolete too:

def display_services
  concat("<div>")
  form_for @user do |f|
    f.text_field ...
  end
  concat("</div>")
end

Is it nessaccary to put the <div> tag into the helper. If you need a helper for embedding something into a block you could use some yield-magic as well:

def block_helper
  concat("<div>")
  yield
  concat("</div>")
end

And use it like this in your view - of course with helpers too:

<% block_helper do %>
  cool block<br/>
  <% display_services %>
<% end %>
Joe
Sweet, I like your solution better, it's cleaner, thanks.
Bob
One other note, if you want your HTML to look pretty, and "\n" to the end of concat.
Bob