【问题标题】:error happens when I try "all" method in datamapper当我在 datamapper 中尝试“all”方法时发生错误
【发布时间】:2010-11-10 04:14:55
【问题描述】:

当我尝试在 Sinatra 中执行此操作时,

班级评论 包括 DataMapper::Resource 属性:id,序列号 属性:正文,文本 属性:created_at,日期时间 结尾 得到 '/show' 做 评论 = 评论.all @comment.each 做 |comment| “#{comment.body}” 结尾 结尾

返回这个错误,

ERROR: undefined method `bytesize' for #<Comment:0x13a2248>

谁能指出我正确的方向?

谢谢,

【问题讨论】:

    标签: sinatra datamapper


    【解决方案1】:

    您收到此错误是因为 Sinatra 获取路由的返回值并将其转换为字符串,然后再尝试将其显示给客户端。

    我建议你使用视图/模板来实现你的目标:

    # 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 %>
    

    或者将你的 cmets 附加到一个字符串变量并在你完成后返回它:

    get '/show' do
      comments = Comment.all
    
      output = ""
      comments.each do |comment|
        output << "#{comment.body} <br />"
      end
    
      return output
    end
    

    【讨论】:

      猜你喜欢
      • 2021-04-23
      • 2022-07-17
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      • 2015-05-17
      • 1970-01-01
      相关资源
      最近更新 更多