Unable to access ENV in Rails worker - ruby-on-rails

I am trying to read an environment variable in my worker(also tried in rake tasks) like:
puts ENV('APILAYER_KEY')
but it throws the following error:
NoMethodError: undefined method `ENV' for #<ExchangeRateUpdater:0x0055c5a7a484f0>
As I can see ENV is supposed to be Ruby method and will be available everywhere. https://ruby-doc.org/core-2.2.0/ENV.html
Has anyone seen this problem?

Change the braces (as it's shown in the docs you've linked to):
ENV['APILAYER_KEY']
ENV is an object. The problem with your code is that Ruby treats it as if it was a method, and "APILAYER_KEY" as an argument passed to it.

Related

Rails Console 'helper' and 'app' Appear Incomplete

Using ruby 2.3.0 and rails 4.2.5.1, I'm trying to figure some things
out using the rails console. I have several instances where app or helper seem like they have a method I want, but I often end up with the NoMethodError message. This happens even in a brand-new application where I've edited nothing and given no special options to the rails new command. For instance:
irb(main):015:0* helper.timez
... here I hit TAB to get:
irb(main):015:0* helper.timezone
... and hit ENTER, only to find:
NoMethodError: undefined method `timezone' for #<ActionView::Base:0xc662b80>
Why would tab-expansion in the console expand to methods that don't
exist? More importantly, if they're supposed to be there, why aren't
they? What can I be doing wrong?

Debug 'env' in Rails initializer

I want to check the value of env[:clearance] to see why something is broken.
How can I do this. I tried puts:
config.user.current = Proc.new { env[:clearance].current_user }
puts "ENV[:CLEARANCE]: #{env[:clearance]}"
but that gets: undefined local variable or methodenv' for main:Object (NameError)`
How can I get some visibility into the env hash in this situation? BTW this is loosely related to the Clearance gem.
It's not clear what problem you are trying to solve or what config.user.current is (it's not Clearance). One problem however is that you are trying to use the method env in the proc where it will not be defined. The proc has its own scope.
If you just want to see what ENV is after the clearance middleware has run the simplest thing to do would be to put a binding.pry or debugger statement inside a controller and access env that way.
The clearance environment is set by middleware in response to a request. It will not be set in an initializer.

Rails/Ruby send method can't build paths with params?

So i found this strange anomaly while working in a gem that we are using internally.
We have this private method
private
def redirect_to_element(element, next_upload)
send("scorecard_#{element.base_class_name.underscore}_path", current_scorecard, current_tab(element, next_upload))
end
Which just builds a path dynamically depending on what element is passed to it. What i would like to do is have those dynamic paths pass some params. But i get this error
undefined method `scorecard_enterprise_development_path(ignore_tracking: true)' for #<#<Class:0x007ff767a702e0>:0x007ff767899a20>
so in the console i tried several things and this is what i found.
>> scorecard_enterprise_development_path
=> "/scorecards/338/enterprise_development"
>> send('scorecard_enterprise_development_path')
=> "/scorecards/338/enterprise_development"
>> scorecard_enterprise_development_path(ignore_tracking: true)
=> "/scorecards/338/enterprise_development?ignore_tracking=true"
>> send('scorecard_enterprise_development_path(ignore_tracking: true)')
!! #<NoMethodError: undefined method `scorecard_enterprise_development_path(ignore_tracking: true)' for #<#<Class:0x007ff767a702e0>:0x007ff767899a20>>
That using the send method to build a path with params will fail. Can anyone explain why this happens?
I am using, ruby -v 1.9.3p327 and rails -v 3.2.16
#send will invoke the method identified by the first argument and pass it any arguments specified.
So you should use the method this way:
send('scorecard_enterprise_development_path', ignore_tracking: true)
See the send documentation

How to access a namespace method in Ruby console?

Found this post Include namespace in Rails 3.1 console but it doesn't seem to work.
The following lib/task defined and it works from the command line: rake namespace_name:task_name.
How to call a method method_name in namespace_name from within the console, without calling the task?
rails console
namespace_name::task_name
NameError: undefined local variable or method 'namespace_name' for main:Object
irb namespace_name
NameError: undefined local variable or method 'namespace_name' for main:Object
Working in Rails 3.07, Ubuntu.
If you want to call a method defined inside a .rake file you do something similar to what #Nate said, but instead of calling the raketask, call the method:
require 'rake'
Rake.load_rakefile 'lib/tasks/namespace_name.rake'
method_name(arg1, arg2)
It feels kind of strange that you don't need to specify the namespaces but I just tried this and it worked.
You're confusing two different kinds of "namespaces" - Ruby modules can perform the task of "namespacing" Ruby code; Rake namespaces are only used within Rake to categorize tasks, and they don't create a module namespace.
The page you linked only works with Ruby module namespaces.
If you want to call Rake tasks from the Rails console, it's a bit more involved...
require 'rake'
Rake.load_rakefile 'lib/tasks/namespace_name.rake'
Rake::Task['namespace_name:task_name'].invoke
Or just call it on the command line from within the Rails console -
%x[rake namespace_name:task_name]

NoMethodError in rails

The extracted source is below :
Showing /usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/templates/rescues/diagnostics.erb where line # raised:
private method `gsub' called for #<NoMethodError: undefined method `closed?' for nil:NilClass>
Extracted source (around line #):
RAILS_ROOT: /home/sharath/Desktop/RORWorkspace/ITPLOW1
It was working before installing Sunspot: A Solr-Powered Search Engine for Ruby.
I am working in Ubuntu 10.04.
I'd need to see the full stacktrace to be sure, but this is actually probably an unhelpful HTTP connection error message bubbling up out of RSolr (the library the Sunspot uses for the low-level Solr connection). Is Solr running (i.e., did you run rake sunspot:solr:start)? Can you access http://localhost:8982/solr/admin ?
What's probably happening is you're attempting to do a substitution on some variable which you thought you were initializing, but neglected to give a real value.
For instance, if you had a form where for a Message and one of the the properties you want is the content, you would normally retrieve that information in the controller with
params[:message][:content]
And if you wanted to filter it, you would do something like
params[:message][:content].gsub(/<[^>]*>/,"")
But if the user didn't enter anything into the content field, the params[:message][:content] variable wouldn't be set. Therefore it's null and you're attempting to do nil.gsub

Resources