Can I make an array of links using link_to in Rails?

前端 未结 5 1898
予麋鹿
予麋鹿 2021-02-19 13:29

I have a list of values in url/title pairs that I\'d like to display. (More specifically, each object has its own list of links, some with 0, some with 1, some with more.) I w

5条回答
  •  太阳男子
    2021-02-19 14:18

    Try putting this in your ApplicationHelper

      def sep(separator)
        ->(a, b){ a << separator.html_safe << b }
      end
    

    Then do this in your template

    links.map{|wl| link_to wl.title, wl.url}.reduce(&sep(", "))
    

    You can use any separator you like this way. You also don't need to call html_safe on the whole thing either. Only the separator. So this method is more secure.

提交回复
热议问题