Query works in console and not works in application - ruby-on-rails

I have the following code:
user = User.find_by(authentication_token: params[:authentication_token])
user.update(ip: nil)
when i try these lines on rails console it works like a charm.
when i try it on my app, it doesn't work at all.
it releases the following error:
undefined method `update' for nil:NilClass
Any help?

by some searching and debugging, i found that '+' isn't appearing in authentication token in the controller, it's replaced by a space, so i found a solution here on stackoverflow says to replace + sign with '%2B' after generating the random token.
and it worked.

Related

NoMethodError in browser but work in Console

I'm a newbie for RoR 3, i have followed instruction from Simple Rails 2 book. I got NoMethodError in browser but when i tried from Console, it works without any error. Can any one help me ?
My full Code can view at Github : https://github.com/iamkevinhuang/shovel/
Error in Browser :
Work in Console :
Can anyone help me ?
Please put action show out site protected block. Example put it in line 11

No implicit conversion of nil into String on MessageVerifier

Recently I received new project (backend of iOS app, actually) that works on Ruby (Rails).
I have a part of code in model (user):
155: def self.create_access_token(user)
156: verifier.generate(user.id)
157: end
After some action that indirectly uses that part of code, in "Passenger" output I see following error that terminates everything:
TypeError (no implicit conversion of nil into String):
app/models/user.rb:156:in `create_access_token'
app/models/user.rb:139:in `access_token'
app/controllers/mailing_controller.rb:68:in `send_charts'
verifier is an instance of ActiveSupport::MessageVerifier
I'm totally sure that user.id contains valid value (I've tested it with $stderr.puts)
I'm completely new to this language, it's hard for me to figure out why this error appears. Hope someone can help.
Thanks!
Well, it may the case user_id is nil. Anyway please check out rubyonrails api and verify generate method.
We can actually dig inside generate method. Use pry-rails gem for easy debugging.
Most likely you did not set the access_token_secret in secrets.yml.

undefined method `with_scope' when trying to create new user

I encounter a problem when trying to create a new user .
When I click on create,
undefined method `with_scope' for #
this shows up.
#user.timezonepref = User.find(#user.parent_id).timezonepref
if #user.save
The error console shows that the problem is in the line 'if #user.save'
I don't know why and when I grep for 'with_scope' under the whole folder , I don't see such function exists in any file.
And I also tried to drop, and re-create the whole database. But it is still not working.
The with_scope is an ActiveRecord method. You will not find it in your source code. Go to Rails console and reproduce the problem. You can also do #user.errors.full_messsages to check for error messages.

Can't create users in rails when using authlogic because of cookies

So, I have a standard rails 3 app with authlogic. If I'm not in the browser (in the console or in the test environment), I can't create user models.
For example:
I have this code either in a rspec test or in my console:
user = User.create(...user attributes...)
And I get this error:
NoMethodError: undefined method `cookies' for main:Object
I've looked all over the internet and can't figure this out. Any help would be greatly appreciated.
EDIT:
So, I looked around some more and found in the documentation to do this:
include Authlogic::TestCase
but then I get this error:
uninitialized constant Authlogic::TestCase
I had this same exact problem. Where in your application did you put the following line:
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
I ran into this error when I placed the line in either the environment.rb file or within a file within my initializer folder.
I eliminated the problem when I placed the line in my ApplicationController instead.

NoMethodError: undefined method 'index' when calling deliver on rails 3.1 mailer

I have set up a staging instance of a rails 3.1 application on Heroku and everything seems to work fine except I'm getting a strange error when I try to send emails. I am using the sendgrid starter addon for email delivery. The full error is below:
NoMethodError: undefined method `index' for #<Mail::Message:0x000000048daf28>
/app/.bundle/gems/ruby/1.9.1/gems/mail-2.3.0/lib/mail/message.rb:1289:in `method_missing'
/app/.bundle/gems/ruby/1.9.1/gems/mail-2.3.0/lib/mail/encodings.rb:117:in `value_decode'
/app/.bundle/gems/ruby/1.9.1/gems/mail-2.3.0/lib/mail/encodings.rb:101:in `decode_encode'
/app/.bundle/gems/ruby/1.9.1/gems/mail-2.3.0/lib/mail/fields/unstructured_field.rb:74:in `do_decode'
if I just generate the message object without calling deliver on it and inspect it everything seems fine. I am not seeing this error on my production app. Can you tell me what this error means and how to resolve it? Thanks.
Got the same error on my local machine using Rails 3.0.11. It happened after I passed some object instead of a string to the mail :to attribute. So be sure that the :to attribute is a string!
mail(to: object.to_s)
A new version of Mail, Mail 2.4.0, was released this weekend. I would recommend upgrading to this latest version and seeing if that has fixed your issue.
This error was the result of using Class instead of Module when defining a mail helper I'd created.
This error happened because I was setting custom headers to an integer value. Using to_s on these values resolved the issue for me.
I had a similar problem. However, the error I received was:
NoMethodError (undefined method `ascii_only?' for nil:NilClass)
My problem was I had:
mail(to: emails,....)
And my variable "emails" was actually an array, instead of being one single string of emails.

Resources