acampbe222 acampbe222 - 7 months ago 11
Ruby Question

NoMethodError (undefined method `delete!' for nil:NilClass):

I've been having a little issue with one of the apps I deployed to Heroku. Locally the app works fine, however on Heroku when I press a button that illicits an Ajax request I get a 500 error. The error is more specifically coming from a method within my controller called adding. Below are the logs from Heroku. Link to app: https://ouinyc.herokuapp.com/

**p.s to see the button that's giving the error you have to sign up, it's the two buttons below the stop and play buttons. **

Curiously this is the error that stands out the most.

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


Below are some of the logs from 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]:


edit
So the issue looks like it has to deal with the controller. That said I don't understand why it's saying the string object is empty.

Controller

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 request

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

Answer

Well, the problem code is the following, please dont' used without a real reason non-pure methods with !. And this line evaluated var to nil, since previous value of var has no prefixed line:

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

so next var.delete! riases the exception:

Well the part of code:

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

can be replaced with (since you are passing JSON encoded value):

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