NoMethodError undefined method `encoding' for nil:NilClass CGI.escape - ruby-on-rails

came across this problem a little earlier today, anyone know what might be going on? I'm a rookie with api's. Tried Googling but not much luck so far..
Error:
NoMethodError in ArtistsController#index
undefined method `encoding' for nil:NilClass
the problem line is:
response = HTTParty.get("https://api.spotify.com/v1/search?q=#{CGI.escape params[:query]}&type=artist")
which makes me think it might be an issue with CGI.escape.
If I enter the following url with a query already at the end everything works fine:
http://localhost:3000/search?utf8=%E2%9C%93&query=Caribou&commit=Search
However, if enter the following url without a query I get the error mentioned above:
http://localhost:3000/search
Guess that explains the nil part, but I don't no how to get past this..
Think that's all the info I need to give for this issue but let me know if more is needed.

If params[:query] isn't defined, then CGI.escape is receiving a nil argument.
Make sure params[:query] is defined if you're referencing it, and/or don't call CGI.escape when searching by type only, and not keyword. (Assuming that's possible--that's what the empty search above seems to imply.)

Related

Using Find Method on an already existing Find Method in Ruby showing NoMethodError

As the question says, I am trying to use the .find Method but I am getting a NoMethodError for Nil Objects.
I understand that this is normal behaviour it makes sense, but is there anyway in which I could improve the code to avoid using the .find twice?
This is how my code looks currently:
#device = Device.find(:all).find{|device| device.uuid == #token.uuid}
RubyMine is highlighting the second .find method:
.find{|device| device.uuid == #token.uuid}
with a warning saying :
Method invocation 'find' may produce 'NoMethodError'
I realise this may not be the best way to go about this. Can anyone please help on suggestions? And maybe tell me whether by removing the second .find() I'd get the same results!
I kindly appreciate all answers.
Try this way
#device = Device.find(:all, :conditions => { :uuid => #token.uuid })

Trouble Accessing Ruby Array and Hash Value

I am making an api call and receiving the following response (it's long, so I'm showing the important part):
... "fields":{"count"_1:["0"],"count_2":["5"]} ...
when I do:
call["fields"]["count_1"]
It returns
["0"]
I need it to give me just the integer. I tried:
call["fields"]["count_1"][0]
And I also tried:
call["fields"]["count_1"][0].to_i
I'm running this in Rails and it gives me the error:
undefined method `[]' for nil:NilClass
But it's not working.
Try as below using String#to_i
call["fields"]["count_1"][0].to_i # => 0
Some tips:
Try wrapping the API response in JSON.parse(...). That is, if you're not making the call via a gem that already does this. This might require 'json'.
Try call['fields']['count_1'].first.to_i
Do some debugging: check the value of call.class, call['fields'].class and call['fields']['count_1'].class. The last one should definitly be an Array.
Add an if clause to check if call['fields'][['count_1'].is_empty?.
Look for typos :)
For some reason the API call was making the zeros nil instead of zero. Thanks for all your help.

Rails 4.0 User Authentication/Signup

I'm currently doing this tutorial:
http://net.tutsplus.com/tutorials/building-ribbit-in-rails/
but tried to update it to 4.0 myself since it seems a bit outdated.
https://github.com/erosenberg/myribbit
One of the things that stands out to me is that the tutorial was using 'attr_accessible' whereas I was using a private method to define the params for users to signup.
The solution doesn't have to match the tutorial (I only just got to the part with Sessions) but I would love to be able to see how to fix this specific error that isn't making sense to me and constantly sending me down a rabbit hole:
ArgumentError in Users#new. wrong number of arguments (1 for 0).
app/controllers/application_controller.rb:7:in `current_user'
Any help is greatly appreciated. Thanks!
EDIT: Here is a screenshot of my error in case nobody else is seeing it:
You have a typo in your code: https://github.com/erosenberg/myribbit/blob/master/app/controllers/application_controller.rb#L7
session [:user_id] should be session[:user_id].

Rails: Production Authlogic Problem with find_using_perishable_token

So I'm pretty puzzled by this one. This works in development mode and had worked in production mode before, but I just got this hoptoad error that said this:
An error has just occurred. View full details at:
http://goldhat.hoptoadapp.com/errors/2418099
Error Message:
NoMethodError: undefined method `reset_perishable_token!' for #
Where:
password_resets#create
[GEM_ROOT]/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb, line 260
Now my password resets and user email verification doesn't work because this method (and the reset_perishable_token! method) is apparently undefined. I can't conceive of where to begin with this problem).
Might make sure you have the perishable_token in your database, if you don't I don't think authlogic defines the method.
I am facing the same issue "NoMethodError: undefined method `reset_perishable_token!'" and it take me 8 hours to figure out. I hope this can help someone in the future.
1) Make sure you have "perishable_token" column
2) Make sure you get the correct user record and call "reset_perishable_token" method
My problem is i get the wrong user record to call the "reset_perishable_token" method.
Before: user = User.where(["users.email = ?", email])
After : user = User.where(["users.email = ?", email]).first

can't dup NilClass when using Sanitize gem

Alright this probably is the worst error I have found ever.
I have two projects, both using same code:
Sanitize.clean(string, Sanitize::Config::BASIC)
but one works and another fails.
Problem is similar to this poor guy's post: https://stackoverflow.com/questions/2724342/cant-dup-nilclass-how-to-trace-to-offender
Could anybody help please?
This will happen if you pass nil into clean() instead of a string. Make sure your string variable is really a string.

Resources