Rails Render Partial in Helper

前端 未结 3 774
一个人的身影
一个人的身影 2021-02-10 22:11

I have been trying to render one of my partials in a helper function located within my controller.

The first issue I encountered was that the helper was returning the e

相关标签:
3条回答
  • 2021-02-10 22:23

    Actually, html_safe should be used like this:-

    <%= display_replies(reply).html_safe %>
    
    0 讨论(0)
  • 2021-02-10 22:28

    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!

    0 讨论(0)
  • 2021-02-10 22:37

    You can use the html_safe method like this

    <%= html_safe display_replies(reply) %>
    
    0 讨论(0)
提交回复
热议问题