NoMethodError in Rails 2.3.2 app - ruby-on-rails

I have this strange error in one of my rails 2.3.2 application.
NoMethodError in Timesheet#index
undefined method '>=' for nil:NilClass
Extracted source (around line #27):
24: for alog in act_logs
25: if alog.user_id == session[:user_id].id
27: if(alog.log_date>=#dt.beginning_of_week()&& alog.log_date<=#dt.end_of_week())
As far as I can guess, I think either alog.log_date or #dt.beginning_of_week is nil. But a quick inspection just before line #27(inspection not included here) shows the following values:
#alog.log_date
Wed, 09 Feb 2011
##dt.beginning_of_week()
Mon, 10 Oct 2011
Now if none of these values are nil why do I get this undefined method >= for nil:NilClass error on line #27. If you guys are wondering why I call this strange....its because
1. If I use == operator, everything's ok. Other operators like >, < generate the same error.
2. This same code is working on another machine.
I don't think that it's a code problem here. Has it got something to do with the Ruby or may be Rails installation problem?

The error means precisely that alog.log_date is nil. alog.log_date>=#dt.beginning_of_week() is the same as alog.log_date.>=(#dt.beginning_of_week()), i.e. >= is just a method call (with some syntactic sugar). If the latter would be nil, you'd instead get something like this:
ArgumentError: comparison of Time with nil failed
You could add an existence check for the date in the beginning of the line:
if (alog.log_date && alog.log_date >= #dt.beginning_of_week() && alog.log_date<=#dt.end_of_week())
However, if you think that date should never be nil you might want to add a default value and/or a presence validation for the field.

Good technique to catch such a bug is to attach debugger
or simplier one is to write values to log, you should just put:
Rails.logger.info( variable_to_log )
before error line
PS
it works with equality operator '==' as nil object has this method
your comparison operator exists for integer class only

Related

Opsworks deployment doesn't have node[:deploy]

My opsworks deployment's node doesnt have [:deploy] object
This is my Chef script
if node[:deploy] === nil
Chef::Log.info("No deployment..")
node[:deploy].each do |app, deploy|
Chef::Log.info("deploy -#{ app }-")
end
elsif
# never goes here
end
i got this error on line 4
undefined method `each' for nil:NilClass (NoMethodError)
firstly, i will advise you to read What does the “===” operator do in Ruby?.
i have a feeling that you meant to use ==, rather than ===. change your triple equal operator to double equal operator and give it a try...
you can use #nil? if you want to make it more readable (depending on your ruby version). change
if node[:deploy] === nil
to
node[:deploy].nil?

rails 4 - active job - wrong number of arguments (0 for 1) even though im passing 1 argument

Here I call the job - passing in procedure:
ProcedureDayAfterJob.set(wait_until: reminder_for_jo).perform_later(procedure)
Here is the job itself - taking procedure as argument:
class ProcedureDayAfterJob < ActiveJob::Base
queue_as :mailers
def perform(procedure)
return if procedure.upcoming?
Admin::NotificationMailer.procedure_day_after(procedure).deliver_later
end
end
Here is method in mailer - taking procedure as argument:
def procedure_day_after(procedure)
#procedure = procedure
mail(
# hiding who im sending to
)
end
And here is the error I'm getting:
2017-12-15T19:25:57.365Z 9222 TID-oxvg7jsso ERROR: !!! ERROR HANDLER
THREW AN ERROR !!!
2017-12-15T19:25:57.365Z 9222 TID-oxvg7jsso ERROR: wrong number of
arguments (0 for 1)
2017-12-15T19:25:57.365Z 9222 TID-oxvg7jsso ERROR:
/Users/foreverlabs/foreverlabs/app/jobs/procedure_day_after_job.rb:4:in
`perform'
I'm passing in the argument to each method so can anybody figure out what's going on?
I have seen this happen due to a typo in the initializer for the Service/Class being called within the job.
It is usually misleading with the single line errors where the only helpful info is the line number and file it is erroring on. With that, and looking at your queue log, it shows it is coming from the 4th line which is inside the perform method itself, and where the code has been redacted.
Check the class you are calling on this line to see if "initialize" is misspelled somehow (I do it from time to time myself),because doing such would cause the constructor to disable, as well as cause the class to expect zero arguments.
I'm sure you have fixed this, but hopefully this will help someone else in the future.

Brakeman generating exceptions on an empty file

After upgrading recently to brakeman 3.3.5 I am getting a similar exception on two files. One is app/helpers/profile_mailer and the exception is
golf_mentor/app/helpers/profile_helper.rb:1 :: parse error on value ":" (tCOLON)
If I delete everything but the surrounding block, so the file becomes
module ProfilesHelper
end
I still get the same exception. If I delete everything in the file, I get the following exception:
undefined method `force_encoding' for nil:NilClass While processing app/helpers/profiles_helper.rb
Looking on stackoverflow, such an error is not reported for brakeman, but it appears to be reported for a number of other apps and is related to ruby 2.2 (I am running 2.2.1).
The file permissions for this file are
-rw-r--r-- 1 Chris staff 4 16 Aug 19:57 profiles_helper.rb
How do I fix this?

How to Calculate Nothing - Nil on Rails

Rails 4, Spree 2.1
The Issue:
Adding an item to cart with the Spree Flexi Variants engraving calculator, I get an error:
undefined method `value' for nil:NilClass
Is this line in my engraving calculator model causing this error?
def compute(product_customization, variant=nil)
return 0 unless valid_configuration? product_customization
# expecting only one CustomizedProductOption
opt = product_customization.customized_product_options.detect {|cpo| cpo.customizable_product_option.name == "inscription" } rescue ''
opt.value.length * (preferred_price_per_letter || 0)
end
def valid_configuration?(product_customization)
true
end
And as for the value method, any ideas on that?
I've tried defining value but nothing seems to work. I have another calculator called amount times contant. This is setup pretty much the same way; however, this one works. Getting kind of confused and way past my deadline.
Here is the github repo with the branch that I am using: https://github.com/jsqu99/spree_flexi_variants/tree/spree-2-1-wip
Trace:
NoMethodError (undefined method `value' for nil:NilClass)
/Users/russellkompinski/Desktop/spree_commerce/spree_flexi_variants/app/models/spree/calculator/engraving.rb:29:in `compute'
/Users/russellkompinski/Desktop/spree_commerce/spree_flexi_variants/app/models/spree/product_customization.rb:11:in `price'
/Users/russellkompinski/Desktop/spree_commerce/spree_flexi_variants/app/models/spree/order_contents_decorator.rb:32:in `block in add_to_line_item'
/Users/russellkompinski/Desktop/spree_commerce/spree_flexi_variants/app/models/spree/order_contents_decorator.rb:32:in `map'
/Users/russellkompinski/Desktop/spree_commerce/spree_flexi_variants/app/models/spree/order_contents_decorator.rb:32:in `add_to_line_item'
/Users/russellkompinski/Desktop/spree_commerce/spree_flexi_variants/app/models/spree/order_contents_decorator.rb:7:in `add'
/Users/russellkompinski/Desktop/spree_commerce/spree_flexi_variants/app/models/spree/order_populator_decorator.rb:36:in `attempt_cart_add'
/Users/russellkompinski/Desktop/spree_commerce/spree_flexi_variants/app/models/spree/order_populator_decorator.rb:17:in `block in populate'
/Users/russellkompinski/Desktop/spree_commerce/spree_flexi_variants/app/models/spree/order_populator_decorator.rb:16:in `each'
/Users/russellkompinski/Desktop/spree_commerce/spree_flexi_variants/app/models/spree/order_populator_decorator.rb:16:in `populate'
/Users/russellkompinski/Desktop/spree_commerce/spree_flexi_variants/app/controllers/spree/orders_controller_decorator.rb:18:in `populate'
The source code you pointed to on github contains a different definition of compute as follows:
which is consistent with your stack trace, since line 29 of engraving.rb involves an invocation of value. I'm not familiar with spree, but based on this code, the detect call is returning nil.

How to run Ruby on Rails 3 with ruby 2.0

Is there an easy fix, how I could continue an old rails 3-0.20 installation under ruby 2.0?
The first error, caused by this line:
<%= stylesheet_link_tag :all %>
is
ActionView::Template::Error (no implicit conversion of nil into String):
An upgrade of the rails version would be the best, but unfortunately it is not possible in my case.
Hotfix the problem with the following line at the end in application.rb
ActionController::Base.config.relative_url_root = ''
I ran into the same issue. After drilling down into the stylesheet_link_tag method, I found that the issue comes from here
# actionpack-3.0.20/lib/action_view/helpers/asset_tag_helper.rb:749
if has_request && include_host && !source.start_with?(controller.config.relative_url_root)
The problem is String#starts_with?. In 1.9.3, that method will handle a nil as an input. 2.0.0 does not allow that.
ruby-1.9.3> 'whatever'.start_with? nil
=> false
ruby-2.0.0> 'whatever'.start_with? nil
TypeError: no implicit conversion of nil into String
It's probably also true that later versions of Rails set the value to '' if it's not set to prevent this issue. The hotfix mentioned above does fix the issue, but the root cause is differences between 1.9.3 and 2.0.0.

Resources