2012-07-23 38 views
0

我在RJS下面的代码:RJS - 红宝石环路内page.alert

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内他们的名字?

感谢您的宝贵帮助

回答

3

使用Array.join

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

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

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

或者如果他们是我会建议做的对象:@ books_by_author.collect {| x | x.title} .join(“\ n”) – Ultimation 2012-07-23 14:36:35

+0

非常感谢:) – Kim 2012-07-23 14:37:20