Sinatra:[...]未定义方法`body ='的NoMethodError#<string:[...]> </string:[...]>

时间:2012-06-01 15:06:19

标签: ruby sinatra

尝试在Sinatra中返回响应时,我收到了NoMethodError。这是错误:

/contact/book-me.php上的NoMethodError

未定义的方法`body ='for#&lt; String:0x00000001911418&gt;

/home/kerrick/.rvm/gems/ruby-1.9.3-p194/gems/sinatra-1.3.2/lib/sinatra/base.rb in body
        response.body = value
/home/kerrick/.rvm/gems/ruby-1.9.3-p194/gems/sinatra-1.3.2/lib/sinatra/base.rb in invoke
        body res
/home/kerrick/.rvm/gems/ruby-1.9.3-p194/gems/sinatra-1.3.2/lib/sinatra/base.rb in call!
      invoke { dispatch! }
/home/kerrick/.rvm/gems/ruby-1.9.3-p194/gems/sinatra-1.3.2/lib/sinatra/base.rb in call
      dup.call!(env)
/home/kerrick/.rvm/gems/ruby-1.9.3-p194/gems/rack-protection-1.2.0/lib/rack/protection/xss_header.rb in call
        status, headers, body = @app.call(env)
[...]

这是相关代码:

# Snipped, but basically populate the @error hash if the form wasn't filled out right
if @error.length == 0
  #Snipped, but basically handle the success case
else
  @response = ''
  @error.each do |x, y|
    @response << "<li>#{y}</li> \n"
  end
  return [400, @response]
end

为什么会这样?

1 个答案:

答案 0 :(得分:1)

在Sinatra中使用@response作为返回值是造成问题的原因。同样的问题是documented on the Ruby on Rails OldWiki,因此不仅仅是Sinatra的具体问题。你应该改变你的代码看起来像这样,它会工作:

else
  @send_errors = ''
  @error.each do |x, y|
    @send_errors << "<li>#{y}</li> \n"
  end
  return [400, @send_errors]
end