error happens when I try “all” method in datamapper

☆樱花仙子☆ 提交于 2019-12-05 09:11:24
Luke Antins

Your getting this error because Sinatra takes the return value of a route and converts it into a string before trying to display it to the client.

I suggest you use a view/template to achieve your goal:

# file: <your sinatra file>
get '/show' do
  @comments = Comment.all
  erb :comments
end

# file: views/comments.erb
<% if !@comments.empty? %>
  <ul>
    <% @comments.each do |comment| %>
      <li><%= comment.body %></li>
    <% end %>
  </ul>
<% else %>
    Sorry, no comments to display.
<% end %>

Or append your comments to a String variable and return it when your done:

get '/show' do
  comments = Comment.all

  output = ""
  comments.each do |comment|
    output << "#{comment.body} <br />"
  end

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