首页 > 解决方案 > Ruby Tokbox API 中 nil:NilClass 的未定义方法“用户”

问题描述

我正在尝试使用 tokbox api,但出现以下错误:

Started GET "/en/stream" for 127.0.0.1 at 2018-06-02 18:38:57 +0800
Processing by UsersController#stream as HTML
  Parameters: {"locale"=>"en"}
  Rendering users/stream.html.erb within layouts/application
  Rendered users/stream.html.erb within layouts/application (3.8ms)
Completed 500 Internal Server Error in 34ms (ActiveRecord: 0.3ms)



ActionView::Template::Error (undefined method `user' for nil:NilClass):
    1: <%= javascript_include_tag "//static.opentok.com/webrtc/v2.2/js/TB.min.js" %>
    2: <h1>Your stream</h1>
    3: <div id="publisher"></div>
    4: Link to your stream: <%= link_to watch_url(@stream.user.id), watch_url(@stream.user.id) %>

app/views/users/stream.html.erb:4:in `_app_views_users_stream_html_erb__3447765015494674598_70186549432640'

我的 User/stream.html.erb 看起来像:

 <%= javascript_include_tag "//static.opentok.com/webrtc/v2.2/js/TB.min.js" %>
    <h1>Your stream</h1>
    <div id="publisher"></div>
    Link to your stream: <%= link_to watch_url(@stream.user.id), watch_url(@stream.user.id) %>

我的用户控制器看起来像:

def stream
    @stream = User.find(params[:id]).stream
    @stream     = current_user.stream || current_user.create_stream
    gon.opentok = opentok_data(@stream)
  end

  def watch
    @stream = User.find(params[:id]).stream
    gon.opentok = opentok_data(@stream)
  end

  private

  def opentok_data(stream)
    token = OpenTokClient.generate_token(stream.opentok_session_id)
    { sessionId: stream.opentok_session_id, apiKey: Figaro.env.opentok_api_key, token: token }
  end

我的用户模型有一个流,我的流模型有一个用户。有谁知道为什么它不识别用户?

标签: ruby-on-rails

解决方案


推荐阅读