ArgumentError in FactoryBot build method in Ruby On Rails - ruby-on-rails

Good morning all,
I am following an online course from Udemy, in the latter portion of the course, we learned to use FactoryBot. At this moment, I'm having an error in irb on FactoryBot.build(:book) method
irb(main):001:0> FactoryBot.build(:book)
Traceback (most recent call last):
1: from (irb):1
ArgumentError (missing keywords: from, to)
I don't find out why, because, first of all, I followed the course to the letter (I came back to it several times). Secondly, I have the same version as the trainer:
2.5.1

Related

NameError (uninitialized constant ActiveStorage::Downloading)

I am running Rails 6.0.3.2 and ruby 2.6.6
I am trying to use the ActiveStorage::Downloading class but can't figure out how to get it to load!
I am getting the error
irb(main):001:0> include ActiveStorage::Downloading
Traceback (most recent call last):
1: from (irb):1
NameError (uninitialized constant ActiveStorage::Downloading)
I'm pretty sure this is me, but I can't figure it out. I am using ActiveStorage already in the project, but want to use the download_blob_to_tempfile method.
First, load the library by using require and include to use the methods of that module.
require 'active_storage/downloading'
include ActiveStorage::Downloading

Why isn't terminal locating my Ruby file? (beginner)

I'm as much of a beginner as one can be. I'm working through The Well Grounded Rubyist. I've installed Ruby using rbenv/homebrew, I'm using the same version (2.5.1) as the book does, but I can't get my first ruby program named c2f.rb to work.
I'm at my wits end here and would appreciate any help figuring out how I can get my terminal to run this program. Thank you!
I believe there is something messed up with my PATH but I don't know how to fix it. When I view my path using echo $PATH my terminal returns:
/Library/Frameworks/Python.framework/Versions/3.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
I have c2f.rb saved in a folder named rubycode, which is within my User folder. When I run $ ruby c2f.rb the terminal returns:
Traceback (most recent call last):
ruby: No such file or directory -- c2f.rb (LoadError)
When I run it within irb, I get the following errors:
Traceback (most recent call last):
4: from /usr/bin/irb:23:in `<main>'
3: from /usr/bin/irb:23:in `load'
2: from /Library/Ruby/Gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
1: from (irb):1
NameError (undefined local variable or method `c2f' for main:Object)
Computers are very good at finding things. So, in 99.99% of cases when a computer tells you that it can't find something, the reason is that the thing really is not there.
So, the very first steps you should check are:
Does there actually exist a file named c2f.rb in the current working directory?
If yes, make sure that it is really named c2f.rb and not, for example, c2f.rb (space at the beginning) or c2f.rb (space at the end) or ϲ2f.rb (with a Unicode U+03F2 GREEK LUNATE SIGMA SYMBOL (ϲ) instead of the Unicode U+0063 LATIN SMALL LETTER C (c)) or something with a zero-width space, zero-width joiner, zero-width non-joiner or anything similar in the middle.

Calling a method with keywords arguments

Ruby 2.6.3
Rails 5.2
def test_method(param1:, param2:, param3:)
end
test_method(param1: "food")
It is giving me the following error message:
Traceback (most recent call last):
2: from (irb):11
1: from (irb):8:in `test_method'
ArgumentError (missing keywords: param2, param3)
I thought the point of using method parameters, is for me to be able to specify the params I want to use, when calling this method. What am I missing? I am using this in a Rails 5.2 application, if that makes a difference.
There are requried keyword arguments (use something:) and optional keyword arguments (use something: default_value). I think required keyword parameters should go before optional ones.

NoMethodError in controller that has methods defined

I finished implementing and testing a controller, then switched back and forth between Git branches and did some merges here and there, etc.
Now I'm unable to use the methods I've defined for the controller, and also very confused, as I'm getting NoMethodError when trying to call them.
Added an edit and solution at the end of the post.
Using Rails version 5.2.3 -
Here I've got my controller defined: app/controllers/paypal_access_token_controller.rb:
class PaypalAccessTokenController < ApplicationController
before_action :authenticate_admin!, only: [:show]
def njurf
puts "#######################"
puts "why can't i call these methods dangit"
puts "#######################"
end
def show
# doesn't really matter
end
def update
# doesn't really matter either
# no syntax errors, I promise
end
end
I'd like the update method to be called when I start the Rails application.
I added PaypalAccessToken.update() to the file config/environments/development.rb - this worked.
Now that I've implemented stuff in other seemingly unrelated parts of the application, I can't call on methods from this controller anymore.
I removed the line from config/environments/development.rb so that I could run the Rails console and added the njurf method to the controller. From the console, I tried calling PaypalAccessTokenController.njurf, and PaypalAccessTokenController.update, but both give me the bespoke NoMethodError.
Here's some proof of concept
Loading development environment (Rails 5.2.3)
irb(main):001:0> PaypalAccessTokenController.update
Traceback (most recent call last):
1: from (irb):1
NoMethodError (undefined method `update' for PaypalAccessTokenController:Class)
irb(main):002:0> PaypalAccessTokenController.update()
Traceback (most recent call last):
1: from (irb):2
NoMethodError (undefined method `update' for PaypalAccessTokenController:Class)
irb(main):003:0> PaypalAccessTokenController.njurf()
Traceback (most recent call last):
1: from (irb):3
NoMethodError (undefined method `njurf' for PaypalAccessTokenController:Class)
So the controller class exists, at least, but I don't know why I'm getting this error - nor do I know how to go about fixing it.
Any help would be appreciated.
edit: This controller only had routes for the show method. I removed this route when I had implemented and tested the update method, because I no longer needed/wanted these methods to be accessible via URLs.
This is what made the methods inaccessible from console.
solution: Either I leave in a route to one of the controller's methods - or, like Ricky Spanish and amit_saxena pointed out below, properly declare the methods as class methods.
Thanks for the replies
You can call it, when you define it with self
def self.njurf
puts "#######################"
puts "why can't i call these methods dangit"
puts "#######################"
end
PaypalAccessTokenController.njurf
#######################
why can't i call these methods dangit
#######################
=> nil
It might look strange, but are you sure you have defined the proper resources on routes?
You cannot access controller methods like that from console as they aren't class methods. You can instead try this:
PaypalAccessTokenController.new.update
but that doesn't mean it's going to work (it probably needs params to work on), but you will not get a NoMethodError. Most probably you messed something in your routes file. You can check the routes using:
bundle exec rake routes
which will tell you exactly which route points to which controller#action and should give you some pointers about what the problem is. Paste the relevant routes here is you are unable to figure it out. Also pay attention to HTTP method (GET/POST/PATCH). You might be using a GET instead of PATCH and getting the error as a result.

Rails 3.1.0.rc4 + Postgres - cannot read from or write to database after reboot

I've just started to write an application using the latest version of Rails and PostgreSQL. I created the database, added required gem, configured database.yml file and started with two models - User (this one used Devise for authentication) and Group. I created an additional controller for start page (simple one - only to display list of links). Everything seemed fine, I was able to add test data to the database - until I came back this morning and wanted to continue work.
As long as I stayed on the home page, everything looked just like yesterday. But when I tried to access group list, I got the following error:
Routing Error
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map
There was no additional informations on the page, so I looked into Webrick console and saw the following:
ActionController::RoutingError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map):
app/controllers/groups_controller.rb:1:in `<top (required)>'
The first line of my controller is, as usual:
class GroupsController < ApplicationController
I looked at the other actions, and the result was the same: unexpected nil object. Same issue occured while trying to perform any action on User.
I suspected it's a database problem (because it didn't affect the controller that wasn't using database at all), so I went to rails console to see if I could add entries manually. I couldn't.
ruby-1.9.2-p180 > group = Group.new
(some SQL)
NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.map
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/persistence.rb:320:in `attributes_from_column_definition'
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/locking/optimistic.rb:69:in `attributes_from_column_definition'
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.1.0.rc4/lib/active_record/base.rb:1525:in `initialize'
from (irb):1:in `new'
from (irb):1
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:45:in `start'
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands/console.rb:8:in `start'
from /home/lite/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.1.0.rc4/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
I looked into the most top file (persistence.rb) and searched for line 320.
319: def attributes_from_column_definition
320: Hash[self.class.columns.map do |column|
321: [column.name, column.default]
322: end]
323: end
This definition gave me a little idea what might be happening, so I ran one more command in the console (Group.inspect) and I got error on the following line:
attr_list = columns.map { |c| "#{c.name}: #{c.type}" } * ', '
It seems like I'm not able to access the columns of my table, but I have no idea why. I'm logged in as the same user, on the same machine, using the same operating system and kernel. Out of curiosity, I created another application and it didn't work after the reboot either.
I've spent now four hours looking for answer, but I couldn't find anything related. What might be causing this problem and how to fix it?
I have found the guilty one: small gem called 'automatic_foreign_key' I used to automatically detect foreign keys. When I rollback the changes it made, removed it and altered the table manually, I had my application working again.
Thank you all for help!
Maybe Group is a reserved word in this version of postgresql or rails. rails-3.1.0-rc4 is the most fresh version of rails. Generate another dead simply model to check it.

Resources