Rails Render Partial in Helper

邮差的信 提交于 2019-12-03 08:41:11

Actually, html_safe should be used like this:-

<%= display_replies(reply).html_safe %>

To fix \n and [", we need to have .join after the loop. Like so:

Helper:

def display_replies(comment)
  if comment.replies.count > 0
    raw(
      comment.replies.map do |reply, index|
        render 'comment', index: index
      end.join
    )
  end
end

View:

<%= display_replies(reply) %>

Note that I removed all html_safe and replaced with raw. And instead of each loop, I used map, so we don't have to create variable string and return it after the loop.

Hope this helps!

You can use the html_safe method like this

<%= html_safe display_replies(reply) %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!