Topic: Output from helper method

Hi all, I have this helper method: module UsersHelper  def user_label

Re: Output from helper method

No sure what's happened to my post. I'll try again...

this helper method:

module UsersHelper
  def user_label
    "<p>test</p>"
  end
end

And I'm expecting the page to render "test", without the p tags.

However, calling this helper only renders the html or raw html (with the tags).

#{user_label.html_safe}  => <p>test</p>
#{raw user_label.html_safe}  => <p>test</p>
#{h user_label}  => 3: &lt;p&gt;test&lt;/p&gt;

Any idea what's going on? Even calling .html_safe in the helper method (and removing that from the partial) still renders the tags.

Last edited by phoxpsh (2013-03-31 10:06:32)

Re: Output from helper method

You're writing a ruby method - you should write your method in ruby - use erb

Re: Output from helper method

Jemagee wrote:

You're writing a ruby method - you should write your method in ruby - use erb

Huh? Which part of my code exactly, 'isn't ruby'?

I really don't think it's efficient to spawn a new instance of an Erb class to render some html from within a helper method, is it?

Re: Output from helper method

It is my understanding that helper_methods are written in ruby - you wouldn't use a helper method just to output basic HTML on every page - you'd use a partial.

If you could be more precise about what you're trying to do - as opposed to theoretical that might help

Re: Output from helper method

Jemagee wrote:

It is my understanding that helper_methods are written in ruby - you wouldn't use a helper method just to output basic HTML on every page - you'd use a partial.

If you could be more precise about what you're trying to do - as opposed to theoretical that might help

I see - yes, I won't simply be outputting simple html like "<p>test</p>" from a helper. I took a lot of the code away in my example to make it easier to illustrate.

The helper will take a model, and iterate over an association and return an attribute, so i end up with something like:

<ul>
  <li>Child model - id = 1</li>
  <li>Child model - id = 2</li>
</ul>

This is done with the code:

def method_name(object)
  content_tag(:ul) do
      object.child_models.collect do |cm|
        "<li>Child model - id = #{cm.id}</li>"  #also tried 'content_tag(:li....' here
      end.html_safe
  end
end

From looking into the tag_helper code in rails, content_tag returns an instance of ActiveSupport::SafeBuffer, and that should be what is returned to the erb template for rendering(?).

However, when returning a SafeBuffer (or even a string) from the helper, I can't get anything but the raw html to output into the page.

Last edited by phoxpsh (2013-03-31 13:34:44)

Re: Output from helper method

Did you try encasing this line "<li>Child model - id = #{cm.id}</li>"  in <%= %> tags?

Re: Output from helper method

Jemagee wrote:

Did you try encasing this line "<li>Child model - id = #{cm.id}</li>"  in <%= %> tags?

No, that line (and the surrounding code in the snippet) is inside my helper method.

Re: Output from helper method

I wonder why your output examples wraps the explicitely unescaped HTML in #{...}:

phoxpsh wrote:
#{user_label.html_safe}  => <p>test</p>
#{raw user_label.html_safe}  => <p>test</p>
#{h user_label}  => 3: &lt;p&gt;test&lt;/p&gt;

Makes me wonder:  What's the template?  Maybe your're concatenating a HTML Safe string to an unsafe one or something and this again forces escaping the HTML markup.