Thursday, 19 September 2013

How do I use MicropostsHelper to wrap posts when they are feed.items? Hartl Ruby On Rails Tutorial

How do I use MicropostsHelper to wrap posts when they are feed.items?
Hartl Ruby On Rails Tutorial

Following the instructions in Hartl's RoR Tutorial Listing 10.47 I made a
MicropostsHelper file to wrap Microposts with very long words.
It works just fine on the user page where Microposts are called with
<%= wrap(micropost.content) %>
But when I try to invoke it on the front page where microposts are called
with
<%= wrap(feed_item.content) %>
it does not work.
I thought perhaps I needed to make an identicle feed_itemHelper but that
didn't work at all. I assume what I need to do is let the
feed_item.content know that it can use the helper but I can't see how to
do that.
Microposts Helper
module MicropostsHelper
def wrap(content)
sanitize(raw(content.split.map{ |s| wrap_long_string(s) }.join(' ')))
end
private
def wrap_long_string(text, max_width = 30)
zero_width_space = "&#8203;"
regex = /.{1,#{max_width}}/
(text.length < max_width) ? text :
text.scan(regex).join(zero_width_space)
end
end

No comments:

Post a Comment