rjs - page.alert里面的ruby循环

时间:2012-07-23 14:29:39

标签: ruby-on-rails rjs

我的rjs文件中有以下代码:

if @books_by_author.count != 0
page.alert("The following books reference the author you want to delete: \n

     # here i want to list all the books referencing the author
");

end

如何遍历@books_by_author并在page.alert中显示其名称?

感谢您的宝贵帮助

1 个答案:

答案 0 :(得分:3)

使用Array.join

page.alert("The following books reference the author you want to delete: \n
            #{@books_by_author.join(', /n')} ");

Ultimation的建议:如果数组包含模型而不是字符串,则需要将它们映射到标题:

page.alert("The following books reference the author you want to delete: \n
            #{@books_by_author.map(&:title).join(', /n')} ");