0

我的 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 中显示他们的名字?

感谢您的宝贵帮助

4

1 回答 1

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')} ");
于 2012-07-23T14:33:47.017 回答