ruby-on-railsrjs

rjs - ruby loop inside page.alert


I have the following code in my rjs file:

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

How do I loop through @books_by_author and display their name inside the page.alert?

Thanks for your precious help


Solution

  • Use Array.join:

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

    Ultimation's suggession: if the array contains models, not strings, you need to map them to the title:

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