How to Calculate Nothing - Nil on Rails - ruby-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.

Related

Rails 5.2, Erratic "Unable to autoload constant": sometimes it works, sometimes not

I have a view that ajax-loads the same partial a number of times. Each one contains the result of a rather extensive calculation which takes a number of seconds. On my machine, about 3 times out of 20, instead of the partial I get the error message "Unable to autoload constant Answers::Node::Questions::KurzSinglePunch... expected .../app/models/answers/node/questions/kurz_single_punch.rb to define it" (I shortened the text and the path for legibility). The file is obviously there, since in 17 of 20 cases it is found, and the partial displays correctly in these cases.
In case this is relevant, I use Puma in single mode on a six-year-old Mac running OS 11.6.
This is the topmost part of the error message:
LoadError in KurzReportsController#company Unable to autoload constant Answers::Node::Questions::KurzSinglePunch,
expected /Users/marion/Documents/Projekte/myproject/app/models/answers/node/questions/kurz_single_punch.rb to define it
Extracted source (around line #511):
#509 else
#510 require_or_load(expanded, qualified_name)
*511 raise LoadError,
"Unable to autoload constant #{qualified_name}, expected #{file_path} to define it" unless from_mod.const_defined?(const_name, false)
#512 return from_mod.const_get(const_name)
#513 end
#514 elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix)
Extracted source (around line #195):
#193 def const_missing(const_name)
#194 from_mod = anonymous? ? guess_for_anonymous(const_name) : self
*195 Dependencies.load_missing_constant(from_mod, const_name)
#196 end
#197
#198 #
We assume that the name of the module reflects the nesting
Extracted source (around line #285):
#283 constant.const_get(name)
#284 else
*285 candidate = constant.const_get(name)
#286 next candidate if constant.const_defined?(name, false)
#287 next candidate unless Object.const_defined?(name)
#288 Rails.root: /Users/marion/Documents/Projekte/myproject
Application Trace app/exporters/kurz_report_extractor.rb:63:
in `infos_for_question_id' app/exporters/kurz_report_extractor.rb:59:
in `block in build_survey_hash' app/exporters/kurz_report_extractor.rb:59:
in `map' app/exporters/kurz_report_extractor.rb:59:
in `build_survey_hash' app/exporters/kurz_report_extractor.rb:15:
in `initialize' app/controllers/kurz_reports_controller.rb:27:
in `new' app/controllers/kurz_reports_controller.rb:27:
in `block in company' app/controllers/kurz_reports_controller.rb:25:
in `each' app/controllers/kurz_reports_controller.rb:25:in `company'
After that there is a long section with a Framework trace.
How can it be that Rails is sometimes unable to find this file? And how can I fix this?

No implicit conversion of Nil to String Error with Paperclip

In my app I've created a view which lists all the PDFs in my S3 Bucket, and for this I am using the Paperclip Gem. However I am now receiving this error in my terminal.
Error performing Courts::SyncronizeBucketJob (Job ID: 8424356c-9717-4cca-9d76-4b157bab1065) from DelayedJob(default) in 450.5ms: TypeError (no implicit conversion of nil into String):
/Users/conorquarry/RubymineProjects/pair/app/_modules/courts/models/ocr_document.rb:46:in `initialize'
/Users/conorquarry/RubymineProjects/pair/app/_modules/courts/models/ocr_document.rb:46:in `new'
/Users/conorquarry/RubymineProjects/pair/app/_modules/courts/models/ocr_document.rb:46:in `block in <class:OcrDocument>'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/interpolations.rb:35:in `block (2 levels) in interpolate'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/interpolations.rb:35:in `gsub!'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/interpolations.rb:35:in `block in interpolate'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/interpolations.rb:34:in `each'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/interpolations.rb:34:in `interpolate'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/attachment.rb:556:in `interpolate'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/attachment.rb:171:in `path'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/storage/s3.rb:355:in `block in flush_writes'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/storage/s3.rb:352:in `each'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/storage/s3.rb:352:in `flush_writes'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-6.0.0/lib/paperclip/attachment.rb:247:in `save'
/Users/conorquarry/.rvm/gems/ruby-2.5.1/gems/paperclip-meta-3.1.0/lib/paperclip-meta/attachment.rb:13:in `save'
Looking at the Paperclip Interpolation Documentation I can't see my error, does anyone have an idea of where I should start? Below is the code throwing me the error.
Paperclip.interpolates :document_folder_name do |a, _s|
Pathname.new(a.instance.object_key).dirname.to_s
end
The error is reproducible by:
Pathname.new(nil)
So in this situation, a.instance.object_key is nil.
From the interpolations doc, a.instance will be the activerecord (or whatever ORM you're using) model that's trying to generate a paperclip path that includes :document_folder_name in it.
I'd guess #object_key is a method defined on your activerecord model and is returning nil.
You can put a pry / debugger or a print statement inside that block to see what the object is, then it should be fairly easy to write a unit spec for that method to ensure it returns the correct string.
In theory calling object_key.to_s would fix the error but would mean that :document_folder_name would interpolate to an empty string when paperclip is calculating the folder path, which may not be desirable.

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?

undefined method `+' for nil:NilClass in sample saml application

Trying to follow the setup here to create a simple SAML application (full project I got here).
I went through and did the setup
bundle install
rails s
This went fine, but when I navigate to
http://localhost:3000/
I get hit with
NoMethodError in SamlController#init
undefined method `+' for nil:NilClass
Extracted source (around line #9):
def init
request = OneLogin::RubySaml::Authrequest.new
direct_to(request.create(saml_settings))
end
def consume
I added some logging to check nil status of request and saml_settings but that seems to return false for both of them
puts request.nil?
puts saml_settings.nil?
Error trace:
Processing by SamlController#init as HTML
false
false
Created AuthnRequest: <samlp:AuthnRequest AssertionConsumerServiceURL='http://localhost:3000/saml/consume' ID='_394fa0a0-f313-0135-85a4-6a0001e18280' IssueInstant='2018-02-13T17:42:45Z' Version='2.0' xmlns:saml='urn:oasis:names:tc:SAML:2.0:assertion' xmlns:samlp='urn:oasis:names:tc:SAML:2.0:protocol'><saml:Issuer>http://localhost:3000/saml/consume</saml:Issuer></samlp:AuthnRequest>
Completed 500 Internal Server Error in 15ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `+' for nil:NilClass):
app/controllers/saml_controller.rb:9:in `init'
(Note: the error is line 5, it says line 9 for me because of debug logging I added)
I'm not too sure what else it could be, not sure what method it can't find and what is nil?
I have never messed with ruby stuff, but from my initial looks I'm not sure where the nil is coming from, the logs show the request being created so not sure. Any help would be appreciated, thanks!
Firstly I think you're using a pretty old ruby-saml gem version. That might be the problem.
I haven't tested your code, but it seems to me that you forgot to set idp_sso_target_url in your settings, and apparently that's the only place it can throw the exception you got. https://github.com/onelogin/ruby-saml/blob/v1.1.2/lib/onelogin/ruby-saml/authrequest.rb#L39
Posting the answer here, turns out that i was using a metadata url for the OKTA_METADATA environment variable.
Had to modify
settings = idp_metadata_parser.parse(ENV['OKTA_METADATA'])
to
settings = idp_metadata_parser.parse_remote(ENV['OKTA_METADATA'])
where
OKTA_METADATA=http://blahblahblah.com/metadata

Why is gsub complaining about nil when my object isn't nil?

I’m using Rails 4.2.7 and have the following to trim all white space from a particular expression …
puts "division: #{division}$$"
division = division.gsub(/\A\p{Space}+|\p{Space}+\z/, '')
when I run this, eventually my program dies with this output …
division: 18-29$$
Error during processing: undefined method `gsub' for nil:NilClass
/Users/mikeb/Documents/workspace/runtrax/app/models/race_time.rb:27:in `block in <class:RaceTime>'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:448:in `instance_exec'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:448:in `block in make_lambda'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:228:in `block in halting_and_conditional'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:506:in `block in call'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:506:in `each'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:506:in `call'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activesupport-4.2.7.1/lib/active_support/callbacks.rb:92:in `__run_callbacks__'
…
I don’t understand why gsub thinks its getting a nil when the line above, the string is clearly not empty (a value of 18-29 is printed out). Curiously, I can’t reproduce this in a Rails console, so I’m wondering it here’s something else going on or if there’s a better way to write the above.
Some thoughts:
gsub is a method on strings. test out the type for "division" with this: puts division.class you may find that while "division" is not "nil," it may not be a string so it's throwing an error. see HERE for more description.
since your code is meant to change the value of division, you can do so more elegantly and economically using a "destructive method" as follows: division.gsub!(/\A\p{Space}+|\p{Space}+\z/, '') See HERE for a discussion of destructive methods (note the "!", also called "bang"). This probably doesn't solve the original question, but it does pretty up your code.

Resources