当前位置:  开发笔记 > 编程语言 > 正文

Rails respond_with输出json并尝试将其称为方法

如何解决《Railsrespond_with输出json并尝试将其称为方法》经验,为你挑选了1个好方法。

我创建了一个简单的api,用json响应.当我尝试从浏览器调用它时,我得到了相应的json响应,但是当我尝试从actionscript远程调用它时,似乎它试图调用json就好像它是一个方法.这是控制器动作:

  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

这是接到电话时的服务器响应

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 #):
  app/controllers/connections_controller.rb:7:in `matcher'

为什么rails试图执行json响应?我不明白.

更新:

这是进行调用的actionscript代码,虽然我不明白为什么它会产生影响.

        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);

谢谢!



1> Roman..:

我很好奇为什么它在浏览器中有效.我希望它也能在那里失败.检查来自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

显而易见,它试图将其用作方法.我想你一直坚持:

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

推荐阅读
ifx0448363
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有