开发者

Trouble on responding to a HTTP Request when counting records

I am using Ruby on Rails 3 and I am trying to implement an API. I would like to开发者_如何学编程 solve some strange problems that I have on returning data after a web client HTTP GET Request. In few words, problems are in the response body returned values for which I get "too much" "" (see the examples belowe) and, sometime, in returning JSON data.

On the web service side in my Rack middleware I have:

class Testing
  def initialize(app)
    @app = app
  end

  def call(env)
    accounts = Account.find([1,2,3])

    resp_test = accounts.count
    [200, {}, resp_test] # No [200, {'Content-Type' => 'application/json'}, resp_test]
  end
end

On the client side if I see the response I have

# debug response.body 
---

In this case the problem is the accounts.count that returns a value of "" in the response body. It is possible that accounts.count doesn't do what it should do.

I also encountered some problems when I didn't return JSON data. For example, debugging variables on the client side, sometime I got a body response value of "" if I didn't return JSON DATA like this:

# On the service side in the Rack middleware file
[200, {}, resp_test] # No[200, {'Content-Type' => 'application/json'}, resp_test.to_json]

The response are:

# Case don't returning JSON data
# debug response.body 
---

# Case returning JSON data
# debug response.body 
--- test_value

What is the problem? If it is accounts.count or Account.find([1,2,3]), how can I make that to work in order to return correct value?


first your middleware should be after ActiveRecord::ConnectionAdapters::ConnectionManagement when calling rake middleware

second try to return resp_test.to_s

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜