Ruby on rails Rails通过输出json并尝试将其作为方法调用来响应_

Ruby on rails Rails通过输出json并尝试将其作为方法调用来响应_,ruby-on-rails,ruby,rest,ruby-on-rails-3,Ruby On Rails,Ruby,Rest,Ruby On Rails 3,我创建了一个用json响应的简单api。当我尝试从浏览器调用它时,我会得到相应的json响应,但当我尝试从actionscript远程调用它时,它似乎试图调用json,就好像它是一个方法一样。这是控制器操作: def matcher @conn = Connection.first if(@conn) respond_with({:conn => @conn, :status => :ok}.to_json) else respon

我创建了一个用json响应的简单api。当我尝试从浏览器调用它时,我会得到相应的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 #<ConnectionsController:0x000001030e4350>):
  app/controllers/connections_controller.rb:7:in `matcher'

谢谢

我很好奇为什么它能在浏览器中工作。我预计它在那里也会失败。 使用以下命令检查响应单元的源:

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_with是一个响应程序,它将对象作为参数,而不是json。它根据操作确定适当的响应


如果您想呈现自己的json,可以使用respond_to.

也可以发布js代码吗?我没有js代码。调用方处于actionscriptUPDATE中:只有在POST no GETT的情况下才会发生上述链接现在已失效。是github上的后期归档。
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