简体   繁体   English

Ruby on Rails NoMethodError:nil:NilClass的未定义方法“ []”

[英]Ruby on Rails NoMethodError: undefined method `[]' for nil:NilClass

I'm new to ruby on rails and currently working on a web application which uses the 'rspotify' gem to interact with the Spotify API. 我是Ruby的新手,目前正在使用“ rspotify” gem与Spotify API交互的Web应用程序上工作。

I have a controller called 'genre' which I use to direct my search results from the index page. 我有一个称为“流派”的控制器,可用于从索引页面引导搜索结果。 However, it won't let me initialize any variables inside the index function. 但是,它不会让我初始化index函数中的任何变量。

This is what my controller looks like 这就是我的控制器的样子

# genre_controller.rb
  def index
    if params[:search]
      @genres = RSpotify::Recommendations.generate(limit: 20, seed_genres: ['country'])
      # render :json => @songs
    else
      redirect_to root_path
    end
  end

This is what my my index page looks like 这是我的索引页面的样子

<h1>Genre#index</h1>
<p>Find me in app/views/genre/index.html.erb</p>

<h1 class="page-header">Music Matching <%= params[:search] %></h1>
<ul>
 <% @music.each do |music| %>
   <li><%= music.name %> | <%= link_to "Show", genre_path(music.id) %></li>
 <% end %>
</ul>

This is what my routes look like 这是我的路线

  get 'genre/index'
  get 'genre/show', to:'genre#show', as: 'genre'

  get 'site/index'
  root 'site#index'

I get an error on line 4 of my genre controller, where I call the RSpotify method. 我的类型控制器的第4行出现错误,我在此调用RSpotify方法。 Any variable I try to instantiate there returns a NoErrorMethod. 我尝试在那里实例化的任何变量都将返回NoErrorMethod。

Any help is appreciated, thanks! 任何帮助表示赞赏,谢谢!

Edit: 编辑:

Here's the exception I get from the console 这是我从控制台得到的例外

NoMethodError (undefined method `[]' for nil:NilClass):

app/controllers/genre_controller.rb:4:in `index'
Started GET "/genre/index?utf8=%E2%9C%93&search=hello" for 127.0.0.1 at 2018-02-19 16:14:59 -0800
Processing by GenreController#index as HTML
  Parameters: {"utf8"=>"✓", "search"=>"hello"}
Completed 500 Internal Server Error in 295ms

Here's the full tracestack from the exception 这是来自异常的完整跟踪堆栈

rspotify (1.27.0) lib/rspotify/recommendations.rb:101:in `initialize'
rspotify (1.27.0) lib/rspotify/recommendations.rb:97:in `new'
rspotify (1.27.0) lib/rspotify/recommendations.rb:97:in `generate'
app/controllers/genre_controller.rb:4:in `index'
actionpack (5.1.4) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.4) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.4) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.4) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.4) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.4) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.4) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.4) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.4) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.4) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.4) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.4) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.4) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.4) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.4) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.4) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.4) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.4) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.4) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.4) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.4) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.4) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.4) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.4) lib/action_dispatch/routing/route_set.rb:834:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.4) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.4) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.4) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.4) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.4) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.4) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.4) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.1.4) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.4) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.4) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.4) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.4) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.4) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.4) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.4) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.4) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.4) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.4) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
railties (5.1.4) lib/rails/engine.rb:522:in `call'
puma (3.11.2) lib/puma/configuration.rb:225:in `call'
puma (3.11.2) lib/puma/server.rb:624:in `handle_request'
puma (3.11.2) lib/puma/server.rb:438:in `process_client'
puma (3.11.2) lib/puma/server.rb:302:in `block in run'
puma (3.11.2) lib/puma/thread_pool.rb:120:in `block in spawn_thread'

I got a similar error as above when I tried to connect to the Spotify API. 当我尝试连接到Spotify API时,出现了与上述类似的错误。 The documentation on rspotify missed out the fact that you need to run the authenticate method before you do anything with the API. 关于rspotify的文档忽略了以下事实:在使用API​​进行任何操作之前,您需要运行authenticate方法。

ie

RSpotify.authenticate("<your_client_id>", "<your_client_secret>")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby On Rails - NoMethodError:nil:NilClass 的未定义方法“[]” - Ruby On Rails - NoMethodError: undefined method `[]' for nil:NilClass nil:NilClass Ruby on Rails的未定义方法&#39;&gt;&#39;(NoMethodError) - undefined method `>' for nil:NilClass Ruby on Rails (NoMethodError) nil 的未定义方法“%”:NilClass (NoMethodError) Ruby on Rails - Undefined method `%' for nil:NilClass (NoMethodError) Ruby on Rails 了解Ruby on Rails:NoMethodError(nil:NilClass的未定义方法“ []”): - Learn Ruby on Rails: NoMethodError (undefined method `[]' for nil:NilClass): Rails 5.2 上的 Ruby - NoMethodError(nil:NilClass 的未定义方法“主机”): - Ruby on Rails 5.2 - NoMethodError (undefined method `host' for nil:NilClass): Ruby未定义方法`[]&#39;表示nil:NilClass(NoMethodError)错误 - Ruby undefined method `[]' for nil:NilClass (NoMethodError) error Ruby - NoMethodError(nil的未定义方法:NilClass): - Ruby - NoMethodError (undefined method for nil:NilClass): Ruby:NoMethodError:nil的未定义方法`&lt;&lt;&#39;:NilClass - Ruby: NoMethodError: undefined method `<<' for nil:NilClass undefined方法[]为nil:ruby中的NilClass(NoMethodError)...为什么? - undefined method [] for nil:NilClass (NoMethodError) in ruby… Why? 未定义的方法“ +”,用于nil:NilClass(NoMethodError)-Ruby - undefined method `+' for nil:NilClass (NoMethodError) - Ruby
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM