简体   繁体   中英

Adding HTML tags (e.g. break tag) to embedded Ruby block

Note: I am a Ruby on Rails newbie so excuse this question if it seems too obvious. But for the life of me, I can't figure out where to add an HTML break tag to this Ruby code to make the elements stack on top of each other, not beside each other.

<p>
<strong>Genres:</strong>
<%= @book.authors.map {|a| a.name}.join(', ') %>
</p>

I have added the break tag after the %> and before it, inside the join method area with the comma, and everywhere else you can imagine and nothing works, it just places the data results beside each other sequentially. I am sure it's something simply but I can't figure it out.

Any help on this would be appreciated.

The typical Rails way to do this is with a for or each loop:

<p>
<strong>Authors:</strong>
<% for author in @book.authors %>
<%= author.name %><br/>
<% end %>
</p>

The for statement could also be @book.authors.each do |author| . From an HTML and CSS perspective, I would argue that it is much better to put each author in their own container element such as <li> , which can be styled to look the way you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM