【问题标题】:Obtaining a render Argument Error: What is the correct syntax or usage?获取渲染参数错误:正确的语法或用法是什么?
【发布时间】:2012-12-06 07:12:36
【问题描述】:

我是 Ruby、Sinatra 和 Pusher 的新手,所以这是一个基本问题。我正在尝试对 Pusher 客户端进行身份验证(使用 iOS 演示代码 https://github.com/lukeredpath/libPusher)。当 iOS 客户端尝试加入在线状态频道时,下面的服务器代码失败并出现错误:

ArgumentError - wrong number of arguments (1 for 2):
    /Users/waveocean/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:665:in `render'
    web.rb:13:in `auth'
    web.rb:26:in `block in <main>'
    /Users/waveocean/.rvm/gems/ruby-1.9.3-p327/gems/sinatra-1.3.3/lib/sinatra/base.rb:1265:in `call'

... snipped for brevity ...

代码如下:

require 'sinatra'
require 'pusher'
require 'thin'
Thin::HTTP_STATUS_CODES[403] = "FORBIDDEN"

Pusher.app_id = 'MY-APP-ID'
Pusher.key = 'MY-KEY'
Pusher.secret = 'MY-SECRET'

def auth
  response = Pusher[params[:channel_name]].authenticate(params[:socket_id], {:user_id => 101})
  render :json => response
end

 use Rack::Auth::Basic, "Protected Area" do |username, password|
   username == 'foo' && password == 'bar'
 end

 post '/presence/auth' do
   if params[:channel_name] == 'presence-demo'
      auth
   else
   #   render :text => "Forbidden", :status => '403'
   response.status = 403
   end
 end

有人可以提供建议或正确使用渲染吗?

【问题讨论】:

    标签: ruby sinatra pusher


    【解决方案1】:

    这是我发现的。 render 与 Rails 相关联,而不是严格意义上的 Ruby。要响应 Sinatra 路由,请在 auth 方法中使用以下内容:

    def auth
      response = Pusher[params[:channel_name]].authenticate(params[:socket_id], {:user_id => 101})
      [200, {"Content-Type" => "application/json"}, response.to_json]
    end
    

    事实证明,Pusher iOS 项目演示提供了一个带有所需实现的 Scripts/auth_server.rb 文件。它在此处与安装说明一起记录:https://github.com/lukeredpath/libPusher/wiki/Adding-libPusher-to-your-project

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多