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
Related
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.
I'm trying to use modules for namespacing reasons. I have this file located in my Rails app at /lib/reports/stripe.rb.
module Reports
module Stripe
def self.foo
puts 'i am foo'
end
end
end
In my console, I'd expect to be able to call foo by Reports::Stripe.foo or Reports::Stripe::foo, but I get the error
NoMethodError: undefined method `foo' for Reports::Stripe:Module
What am I doing wrong? Also feel free to let me know if there's a better way to organize the location and namespacing.
All method calls in ruby use the . syntax. Even "module" methods.
> Reports::Stripe.foo
i am foo
You may be receiving the error NoMethodError: undefined method 'foo' for Reports::Stripe:Module if you have added the method after you have started the rails console. Try restarting the console or reloading the file with load 'reports/stripe'.
The file was actually located in /lib/reports/stripe/stripe.rb. It was a mistake I made much earlier, but forgot to fix. Moving the file to /lib/reports/stripe.rb resolved the issue.
Ruby nonce throwing error
require 'date'
nonce = DateTime.now.to_i
Error:
undefined method `to_i' for #<DateTime:0x000000015336e8> (NoMethodError)
Working in my console it gives correct value
2.1.0 :014 > nonce = DateTime.now.to_i
=> 1405065242
Why it throws error programatically?
EDIT
Is there any way to add nonce. The condition is it should be a integer that has to be incremented on every subsequent request
As Pavan sais, if you run your code in irb it probably will not working anymore.
According to the doc, Ruby hasn't method to_i in DateTime class.
However, Rails override DateTime class to have a to_i method:
So, I think you run the command whick works in an Ruby On Rails environment, that's why it works. But if you run in a Ruby environment without Rails, it will not work.
Hope it helps.
I have written a spec file for update function of one controller. I am using ruby-1.9.3 p194, Rails 3.2.13 and rspec 2.13.1 and data mapper version ~> 1.2.0'. I am calling update function in my spec as follows:
put :update, :id=>#xyz.id, :xyz=> {key1:val1, key2:val2}
In update function of my xyz controller I have accessed id as follows:
def update
params[:xyz][:id] = params[:id] #This line is working fine and params[:id] is not nil
params = params[:xyz]
id = params[:id]
end
Problem is that while I am running the same file without any change twice at a time, it is giving two different errors. Once it is giving the following error:
/home/joy/.rvm/gems/ruby-1.9.3-p194#packageName/gems/dm-core-1.2.0/lib/dm-
core/collection.rb:1302: in resource undefined method 'saved?' for nil:NilClass
(NoMethodError)
....
Next time, while running, it is giving the following error:
NoMethodError:
undefined_method 'id' for "57":string"
# ./app/controllers/xyz_controller.rb : in line number 3 of update where I am setting the id is giving error.
Is this the case the ruby version or the rspec or data-mapper version I am using is unstable or there is any other problem that I could not figure out. Please help.
In order to track activity in my application, I built a set of helpers which take in arrays of data and spit out .csv files. I want to use these from the console like so:
helper.export_data(array_of_data)
This works fine on my machine, but the heroku console doesn't seem to let me call helper functions. I receive the error:
NameError: undefined local variable or method `helper' for main:Object
I would guess here that your application is doing some sort of initialisation to get helper loaded into your console, and that that initialisation isn't occurring when you spin up a Heroku console.
Is there anything that you have done in your code which initialises this helper object?
Had same question, and found the answer here:
http://www.funonrails.com/2011/03/accessing-view-helpers-routes-in-rails.html
relevant part is:
>> include ActionView::Helpers
>> => Object
>> include ApplicationHelper
>> => Object
>> include ActionView::Helpers::ApplicationHelper^C
>> display_amount 2500 => "$2,500"