开发者

Rails respond_with outputting json and trying to call it as a method

I created a simple api that responds with json. When I try to call it from a browser, I get the appropriate json response, but when I try to call it remotely from actionscript, it seems like it trys开发者_开发问答 to call the json as if it is a method. This is the controller action:

  def matcher
    @conn = Connection.first
    if(@conn)
      respond_with({:conn => @conn, :status => :ok}.to_json)
    else
      respond_with({:status => :no_content}.to_json)
    end
  end

And this is the server response when it gets the call

Started POST "/connection/matcher/1.json" for 127.0.0.1 at 2010-11-11 22:57:24 -0800
  Processing by ConnectionsController#matcher as JSON
  Parameters: {"stratus_string"=>"bad1de003755eaa01a2920f0091d0dd66beaf2d34f651b09a578afb1b54e5686", "user_id"=>"1", "id"=>"1"}
  Connection Load (0.5ms)  SELECT "connections".* FROM "connections" LIMIT 1
Completed   in 24ms

NoMethodError (undefined method `{"conn":{"created_at":"2010-11-12T06:55:13Z","id":6,"stratus_string":"474735730abe81d7622d377bd0bf816c3f94721ece3eddf670bf3a74b1c2356c","updated_at":"2010-11-12T06:55:13Z","user_id":1},"status":"ok"}_url' for #<ConnectionsController:0x000001030e4350>):
  app/controllers/connections_controller.rb:7:in `matcher'

Why is rails trying to execute the json response? I don't get it.

UPDATE:

Here is the actionscript code that makes the call, although I don't see why it makes a difference.

        this.restService = new HTTPService();
        this.restService.url = "http://localhost:3000/connection/matcher/1.json";
        this.restService.method = "POST";

        this.restService.addEventListener("result", onRestResult);
        this.restService.addEventListener("fault", onRestFault);

        var request:Object = new Object();
        request.user_id = user;
        request.stratus_string = id;
        this.restService.cancel();
        this.restService.send(request);

Thanks!


I'm curious why it works in the browser. I would expect it to fail there as well. Check the source for the respond_with:

def respond_with(*resources, &block)
  raise "In order to use respond_with, first you need to declare the formats your " <<
        "controller responds to in the class level" if self.class.mimes_for_respond_to.empty?

  if response = retrieve_response_from_mimes(&block)
    options = resources.extract_options!
    options.merge!(:default_response => response)
    (options.delete(:responder) || self.class.responder).call(self, resources, options)
  end
end

It's apparent why it tries to use it as a method. I guess that you're stuck with:

respond_to do |format|
  format.html # some.html.erb
  format.json  { render :json => {:conn => @conn, :status => :ok}.to_json }
end


respond_with is a responder and takes an object as a parameter, not json. It determines the appropriate response based on the action.

http://ryandaigle.com/articles/2009/8/10/what-s-new-in-edge-rails-default-restful-rendering

If you want to render your own json, you use respond_to.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜