问题
The code in the view shall give the books in the database
<% @bookmark.each do |book| %>
works only for multiple rows. If i had single row, it would show error like
" undefined method `each' for #<Bookmark:0x3e2e3f0> "
What should i do to print single row also. ?
回答1:
<% @array_bookmark = @bookmark.class == Array ? @bookmark : [@bookmark] %>
<% @array_bookmark.each do |book| %>
回答2:
Just wrap @bookmark
in Array()
, like this:
<% Array(@bookmark).each do |book| %>
Incidentally you should really rename @bookmark
to @bookmarks
.
来源:https://stackoverflow.com/questions/13836591/rendering-issue-in-views-in-ruby-on-rails