Rendering issue in views in ruby on rails

妖精的绣舞 提交于 2019-12-08 05:13:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!