I'm trying to test a method in my console, but even the basic pluralize -
pluralize(1, 'person')
wont work..
Output:
NoMethodError: undefined method 'pluralize' for main:Object
from (pry):42:in '<main>'
but helper.method(:pluralize) shows me : Method: ActionView::Base(ActionView::Helpers::TextHelper)#pluralize
What am i missing?
The helpers aren't included by default in the console. You can include them first and it'll work:
>> include ActionView::Helpers::TextHelper
>> pluralize(1, 'person')
# => "1 person"
Or, you can use the helper object which Rails gives you in the console:
>> helper.pluralize(1, 'person')
# => "1 person"
Related
Say I have:
app/models/food/fruit.rb:
module Food
class Fruit
class_attribute :field_mapping
self.field_mapping = MyOrg::Trees::Details.field_mapping
# other things including:
end
end
In local/development mode, I can open a console and do/get this:
rails console
[1] pry(main)> Food::Fruit
NameError: uninitialized constant MyOrg::Trees::Details
MyOrg::Trees::Details will eventually be defined via a gem, but until then I want to fake it by adding the following to the top of the same file; why isn't this doing the trick?
module MyOrg
module Trees
class Details
def field_mapping
end
end
end
end
Note: when I paste the above into a local/development console, I can then successfully do this:
[2] pry(main)> MyOrg::Trees::Details.new.field_mapping
=> nil
When I added the above fake, I accidentally started trying to reference it to test what I was doing...
rails console
[1] pry(main)> MyOrg::Trees::Details
NameError: uninitialized constant MyOrg::Trees::Details
...but I should have called Food::Fruit as before, and I would have isolated the issue...
rails console
[1] pry(main)> Food::Fruit
NoMethodError: undefined method `field_mapping' for MyOrg::Trees::Details:Class
...ah, I need to make it a class method...
module MyOrg
module Trees
class Details
def self.field_mapping
end
end
end
end
...and then...
rails console
[1] pry(main)> Food::Fruit
NoMethodError: undefined method `values' for nil:NilClass
So, I appeased the undefined method error, and now have a new problem, which is that further down the original code there is actually another reference to field_mapping (field_mapping.values) which now has to be appeased...
module MyOrg
module Trees
class Details
def self.field_mapping
{}
end
end
end
end
...and now...
rails console
[1] pry(main)> Food::Fruit
=> Food::Fruit
In config/environments/production.rb (and the other environments) there is:
config.eager_load = true
and a whole bunch of other config.foobar calls. But where does 'config' come from? Usually you have something like:
SomeClass.each do | block_variable |
block_variable.some_method
end
but in the case of the mystical 'config' this is not the case, its like a block variable which is not declared anywhere. Opening rails console, when I do:
irb(main):001:0> config
NameError: undefined local variable or method `config' for main:Object
Did you mean? conf
from (irb):1
irb(main):002:0>
and the same result occurs for app.config
irb(main):002:0> app.config
NoMethodError: undefined method `config' for #<ActionDispatch::Integration::Session:0x00007fc898d02808>
from (irb):2
How is it possible for ruby to allow calls on 'config'?
These classes include the ActiveSupport::Configurable module:
https://api.rubyonrails.org/classes/ActiveSupport/Configurable.html
Here is an alternative to implementing this behavior by yourself:
https://robots.thoughtbot.com/mygem-configure-block
I have these models:
Class A
embeds_many :b
end
Class B
belongs_to :c
end
Class C
end
I'm working with rails_admin and mongoid. In admin, when I try to retrieve the list of C records when I'm creating an A instance I'm getting this error:
This only happens on production envirnment not in development
NoMethodError (undefined method `associations' for nil:NilClass):
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid/abstract_object.rb:10:in `initialize'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid.rb:24:in `new'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/lib/rails_admin/adapters/mongoid.rb:24:in `get'
/home/pablo/.rvm/gems/ruby-2.3.0#mh-backend/bundler/gems/rails_admin-355dc80f8a20/app/controllers/rails_admin/main_controller.rb:138:in `get_association_scope_from_params'
Taking a look at rails_admin code we can see that piece of code in mongoid.rb file.
def get(id)
AbstractObject.new(model.find(id))
rescue => e
raise e if %w(
Mongoid::Errors::DocumentNotFound
Mongoid::Errors::InvalidFind
Moped::Errors::InvalidObjectId
BSON::InvalidObjectId
).exclude?(e.class.to_s)
end
If we pay attention at this code we can see that model.find(id) must produce a Mongoid::Errors::DocumentNotFound if document doesn't exists by default.
However, in mongoid you can avoid the raise of this error with raise_not_found_error: true in mongo config file, this produce the undefined method of nil class.
Tracking issue on github
I would like to be able to use toggle but not quite sure how to do this.
I have a boolean attribute called deceased on my Member model.
Yet when I try to toggle it with toggle(attribute) it doesn't work.
This is what happens at the command-line:
[41] pry(main)> member.deceased.toggle!
NoMethodError: undefined method `toggle!' for true:TrueClass
from (pry):41:in `__pry__'
[42] pry(main)> member.toggle(deceased)
NameError: undefined local variable or method `deceased' for main:Object
from (pry):42:in `__pry__'
[43] pry(main)> member.deceased.toggle
NoMethodError: undefined method `toggle' for true:TrueClass
from (pry):43:in `__pry__'
[44] pry(main)> member.deceased
=> true
[45] pry(main)> toggle(member.deceased)
NoMethodError: undefined method `toggle' for main:Object
from (pry):45:in `__pry__'
How do I use toggle?
FYI: All of the above was done at the rails c...but I have pry installed. But given that toggle seems to be an ActiveRecord method, it shouldn't be an issue right?
You should be able to do it with
member.toggle(:deceased)
Here is the code
#add email of team lead of the current user
def add_the_tl_email(to_address, session_user_id)
ul = UserLevel.find_by_user_id(session_user_id)
if !ul.team.nil? && !ul.role.nil?
User.user_status('active').joins(:user_levels).where(:user_levels => {:position => 'tl',
:role => ul.role,
:team => ul.team }).each do
|u| to_address << u.email
end
end
end
The error from spec:
1) CustomersController GET customer page 'create' successful should create one customer record
Failure/Error: post 'create', :customer => #customer
NoMethodError:
undefined method `team' for nil:NilClass
# ./app/mailers/user_mailer.rb:36:in `add_the_tl_email'
# ./app/mailers/user_mailer.rb:15:in `notify_tl_dh_ch_ceo'
# ./app/controllers/customers_controller.rb:27:in `create'
# ./spec/controllers/customers_controller_spec.rb:48:in `block (5 levels) in <top (required)>'
# ./spec/controllers/customers_controller_spec.rb:47:in `block (4 levels) in <top (required)>'
team is a field in user_level. Here is the user_level in factory:
Factory.define :user_level do |level|
level.role "sales"
level.position "member"
level.team 1
level.association :user
end
In rails console, the ul.team.nil? returns either true or false on test data generated by factory.
Since team is a field in user_level, why there is error in spec like: NoMethodError:
undefined method `team' for nil:NilClass ?
Thanks.
The issues is that nil.team is not a method*
NoMethodError:
undefined method `team' for nil:NilClass
That is, ul evaluates to nil: check ul.nil? before checking ul.team.nil?... but might want to find out why the find failing to start with ;-)
Happy coding.
*This is what the real error message says, the title of the post is garbage :-/
The call in
ul = UserLevel.find_by_user_id(session_user_id)
returned nil and thus nil was assigned to ul.
So when you called
!ul.team.nil?
Ruby tried to call team on nil. nil is an instance of NilClass and there is no method named team on NilClass.