Rails Render Partial in Helper

前端 未结 3 791
一个人的身影
一个人的身影 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: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!

提交回复
热议问题