首页 文章

列出视图中连接方法的项目

提问于
浏览
0

我有一个方法连接我的数组中的项目,当它们被调用到视图时,它们被显示为一个句子,所有这些都在一行上 .

def ingredient_names(ingredients)
if ingredients
  ingredient_array = ingredients.map {|ing| ing.ingredient_name}
  ingredient_array.join("\n")
end
end

我将如何让数组中的每个项目显示为列表?所以例如

flour
eggs
water

有没有这方法或我会改变

("\n)

谢谢

1 回答

  • 1

    试试这个

    def ingredient_names(ingredients)
      return ''  if !ingredients
    
      ingredients.map(&:ingredient_name)*'
    ' end

    在视图中称之为

    <%= raw(ingredient_names(ingredients)) %>
    

相关问题