NoMethodError(未定义的方法`删除!'对于nil:NilClass):

时间:2016-04-25 22:34:34

标签: ruby-on-rails ruby ajax heroku

我在部署到Heroku的其中一个应用程序时遇到了一些问题。本地应用程序工作正常,但是当我按下违反Ajax请求的按钮时,在Heroku上我得到500错误。错误更具体地来自我的控制器中称为添加的方法。以下是Heroku的日志。链接到应用: https://ouinyc.herokuapp.com/

** p.s要查看提示您必须注册的错误的按钮,它是停止和播放按钮下方的两个按钮。 **

奇怪的是,这是最突出的错误。

  NoMethodError (undefined method `delete!' for nil:NilClass):
  app/controllers/static_pages_controller.rb:27:in `add_song' 

以下是Heroku的一些日志

2016-04-25T22:28:09.603876+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
2016-04-25T22:28:09.603878+00:00 app[web.1]:   vendor/ruby-2.2.4/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
2016-04-25T22:28:09.603874+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
2016-04-25T22:28:09.603875+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/engine.rb:518:in `call'
2016-04-25T22:28:09.603879+00:00 app[web.1]:   vendor/ruby-2.2.4/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
2016-04-25T22:28:09.603877+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/content_length.rb:15:in `call'
2016-04-25T22:28:09.603878+00:00 app[web.1]:   vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service'
2016-04-25T22:28:09.603880+00:00 app[web.1]:   vendor/ruby-2.2.4/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
2016-04-25T22:28:09.603881+00:00 app[web.1]: 
2016-04-25T22:28:09.603881+00:00 app[web.1]: 
2016-04-25T22:28:11.095771+00:00 app[web.1]:   Rendered layouts/_shim.html.erb (0.4ms)
2016-04-25T22:28:11.133322+00:00 app[web.1]:   Rendered layouts/_audio_part.html.erb (7.2ms)
2016-04-25T22:28:11.133624+00:00 app[web.1]: Completed 200 OK in 50ms (Views: 45.6ms | ActiveRecord: 3.8ms)
2016-04-25T22:28:11.107578+00:00 app[web.1]:   Rendered layouts/_header.html.erb (7.9ms)
2016-04-25T22:28:11.105471+00:00 app[web.1]:   User Load (3.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
2016-04-25T22:28:11.120143+00:00 app[web.1]:   Rendered layouts/_footer.html.erb (2.8ms)
2016-04-25T22:28:11.090234+00:00 app[web.1]:   Rendered static_pages/home.html.erb within layouts/application (0.6ms)
2016-04-25T22:28:11.081117+00:00 app[web.1]: Started GET "/home" for 98.14.169.85 at 2016-04-25 22:28:11 +0000
2016-04-25T22:28:11.083194+00:00 app[web.1]: Processing by StaticPagesController#home as HTML
2016-04-25T22:28:11.135758+00:00 heroku[router]: at=info method=GET path="/home" host=ouinyc.herokuapp.com request_id=2be011c5-bff4-4ac6-8c0b-a681f7826f32 fwd="98.14.169.85" dyno=web.1 connect=0ms service=63ms status=200 bytes=7100
2016-04-25T22:28:23.338889+00:00 heroku[router]: at=info method=GET path="/90210%20by%20Travis%20Scott.mp3" host=ouinyc.herokuapp.com request_id=b8770d58-15e2-40d9-a002-8ddd4fb9d4db fwd="98.14.169.85" dyno=web.1 connect=0ms service=166ms status=206 bytes=8526220
2016-04-25T22:28:41.387156+00:00 heroku[router]: at=info method=POST path="/adding" host=ouinyc.herokuapp.com request_id=5c6bd67b-2c43-4086-90ff-b0fd0f524cd9 fwd="98.14.169.85" dyno=web.1 connect=0ms service=58ms status=500 bytes=1754
2016-04-25T22:28:41.375423+00:00 app[web.1]: Started POST "/adding" for 98.14.169.85 at 2016-04-25 22:28:41 +0000
2016-04-25T22:28:41.377153+00:00 app[web.1]: Processing by StaticPagesController#add_song as */*
2016-04-25T22:28:41.377249+00:00 app[web.1]:   Parameters: {"data_value"=>"\"https://ouinyc.herokuapp.com/90210%20by%20Travis%20Scott.mp3\"", "authenticity_token"=>""}
2016-04-25T22:28:41.379928+00:00 app[web.1]:   User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
2016-04-25T22:28:41.381122+00:00 app[web.1]:   User Load (0.5ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", 1]]
2016-04-25T22:28:41.382269+00:00 app[web.1]: Completed 500 Internal Server Error in 5ms (ActiveRecord: 1.0ms)
2016-04-25T22:28:41.385136+00:00 app[web.1]: 
2016-04-25T22:28:41.385145+00:00 app[web.1]: NoMethodError (undefined method `delete!' for nil:NilClass):
2016-04-25T22:28:41.385146+00:00 app[web.1]:   app/controllers/static_pages_controller.rb:27:in `add_song'
2016-04-25T22:28:41.385147+00:00 app[web.1]: 

修改 所以这个问题看起来必须要处理控制器。这就是说我不明白为什么它说字符串对象是空的。

控制器

class StaticPagesController < ApplicationController

  respond_to :js, :json, :html

     def get_songs()
          if user_signed_in?
                session[:user_id] = current_user.id
                present_user = User.find(session[:user_id])
                present_user = present_user.playlist.keys
            @songs = present_user
        respond_to do |format|
     format.json  { render :json => @songs}
      end
        end
        end



    def add_song()
           if user_signed_in?
                session[:user_id] = current_user.id
                present_user = User.find(session[:user_id])
                var = params[:data_value]
        var = var.sub!("http://localhost:3000/", "")
                var.delete!('""','')
           present_user.playlist.store(var, 1)      
                if present_user.save
                         render json: {success: "It works"}
   end    
      end
        end 

        def remove_song()
            if user_signed_in?
              session[:user_id] = current_user.id
              present_user = User.find(session[:user_id])
              var = params[:data_value] 
              present_user.playlist.delete(var)
       if present_user.save
                     render json: {success: "It works"}
        end 
        end 
        end

  def news
  end

  def home
   end


end

ajax请求

$.ajax({
       url: "removing",
        type: "post",
       data: {data_value: JSON.stringify(player.src), authenticity_token: "<%= @form_auth_token %>" }
        }
   );

1 个答案:

答案 0 :(得分:1)

嗯,问题代码如下,请不要&#39;没有真正原因使用!的非纯方法。此行评估varnil,因为之前的var值没有前缀行:

var = var.sub!("http://localhost:3000/", "")

所以下一个var.delete!会导致异常:

代码的一部分:

var = params[:data_value]
var = var.sub!("http://localhost:3000/", "")
var.delete!('""','')
present_user.playlist.store(var, 1)

可以替换为(因为您传递的是JSON编码值):

song = JSON.parse((params[:data_value]).sub("localhost:3000/", ""),
   {:quirks_mode => true})'
present_user.playlist.store(song, 1)