Hanami: undefined method `size' for nil:NilClass - ruby

I keep learning hanami and ran into the following problem: when I initialize and check the parameters, I encounter the following error
Boot Error
Something went wrong while loading /Users/anewaccount/Projects/Mediateka/config.ru
NoMethodError: undefined method `size' for nil:NilClass
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/deprecated.rb:11:in `input_processor'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/class_interface.rb:165:in `default_options'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/dry-validation-0.11.0/lib/dry/validation/schema/class_interface.rb:35:in `new'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-validations-1.3.7/lib/hanami/validations.rb:109:in `validations'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/params.rb:152:in `params'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `block in params'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `class_eval'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-controller-1.3.3/lib/hanami/action/validatable.rb:100:in `params'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:11:in `<class:SignUp>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:9:in `<module:Auth>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:8:in `<module:Controllers>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:7:in `<module:Web>'
/Users/anewaccount/Projects/Mediateka/apps/web/controllers/auth/sign_up.rb:6:in `<top (required)>'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `require_relative'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `block in require!'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:94:in `each'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:94:in `for_each_file_in'
/Users/anewaccount/.rvm/gems/ruby-2.7.2/gems/hanami-utils-1.3.8/lib/hanami/utils.rb:56:in `require!'
my action:
# frozen_string_literal: true
require 'pry-byebug'
require_relative 'auth_base_action'
module Web
module Controllers
module Auth
class SignUp < AuthBaseAction
params do
required(:user).schema do
required(:first_name).filled(:str?)
required(:last_name).filled(:str?)
required(:email).filled(:str?, format?: /#/)
required(:password).filled(:str?).confirmation
end
end
def call params
binding.pry
end
end
end
end
end
I wrote the params block according to the example from the official documentation on the idea that it should work, but I catch NoMethodError: undefined method `size 'for nil: NilClass I also saw in the official documentation that these validations are delegated from Hanami :: Validations, I installed gem' hanami- validation 'according to their official documentation, but I had the same error, then I started looking at other people's code and they were successful using the example from the official documentation. Please tell me what is the error or please explain what am I doing wrong? I'm already very confused

Related

Celluloid 0.17.3 giving unexpected "undefined method" error

I have started using Celluloid gem this morning for that first time. I am following this Railscasts tutorial and trying to figure things out.
I have a class called "SomeClass" and it has only one method. Here is the code:
require 'celluloid'
class SomeClass
include Celluloid
def initialize(name)
#name = name
end
def assholify()
puts "#{#name} has become an ASSHOLE."
end
end
When I create new instances of the class and call its method (with a bang i.e. "assholify!"), I am getting the undefined method 'assholify!', error. But Celluloid is supposed to trigger the method asynchronously when it is called with a bang. So here is how I am calling the method:
names = ['John', 'Tom', 'Harry']
names.each do |name|
n = SomeClass.new name
n.assholify!
end
Here is the full backtrace of the error:
I, [2016-09-09T11:28:02.488618 #3682] INFO -- : Celluloid 0.17.3 is running in BACKPORTED mode. [ http://git.io/vJf3J ]
/home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:42:in `rescue in check': undefined method `assholify!' for #<SomeClass:0x10897dc> (NoMethodError)
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:39:in `check'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/calls.rb:26:in `dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:16:in `dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:50:in `block in dispatch'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/cell.rb:76:in `block in task'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/actor.rb:339:in `block in task'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task.rb:44:in `block in initialize'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/task/fibered.rb:14:in `block in create'
from (celluloid):0:in `remote procedure call'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/call/sync.rb:45:in `value'
from /home/railsdev/.rvm/gems/ruby-2.3.1/gems/celluloid-0.17.3/lib/celluloid/proxy/sync.rb:22:in `method_missing'
from some_class.rb:18:in `block in <main>'
from some_class.rb:16:in `each'
from some_class.rb:16:in `<main>'
Why am I getting this error? Is it the right way to call the function? Also how do I get rid of Celluloid 0.17.3 is running in BACKPORTED mode. warning?
The undefined method error occurred because actor methods are not called with a bang in the recent versions of celluloid gem. Instead you call the method like this: n.async.assholify. So here is what the code should look like:
names = ['John', 'Tom', 'Harry']
names.each do |name|
n = SomeClass.new name
n.async.assholify # Instead of "n.assholify!"
end
For "Celluloid 0.17.0 is running in BACKPORTED mode" warning, take a look at this wiki. Backported Mode is the default, for a limited time. If you use require 'celluloid/current' instead of require 'celluloid', you should not see this warning.

Rspec, capybara getting started issues

I have have started with simple example of Rspec,Capybara. I have come across few issues. This issues are probably because I have experience with cucumber and page_object gem, but here I am using capybara and Site_prism gem.
I have tried:
my_example_spec.rb
require_relative 'Support/spec_helper'
require_relative 'pages/login_page'
describe 'My behaviour' do
it 'should do something'do
#login_page = LoginPage.new
#login_page.load
#login_page.login('autouser','password')
end
end
and login_page.rb
class LoginPage < SitePrism::Page
set_url "/login"
element :username, "input[id='username']"
element :password, "input[id='password']"
element :submit, "input[id='submit']"
def login(username,password)
#login_page.username.set username
#login_page.password.set password
#login_page.submit.click
end
end
Issues are:
When I run my_example_spec.rb it gives error
Testing started at ...
Run options: include {:full_description=>/My\ behaviour\ should\ do\ something/}
NoMethodError: undefined method `username' for nil:NilClass
./pages/login_page.rb:10:in `login'
./my_example_spec.rb:11:in `block (2 levels) in <top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Shouldn't it be on(LoginPage).login (autouser, password). It should navigate to the page and run login method. It is how it works in page_object gem whats the equivalent of site_prism gem
The login method in your LoginPage class should be
def login(username,password)
username.set username
password.set password
submit.click
end
#login_page is not an instance variable of the LoginPage class so it is not accessible inside the class. It's also not necessary inside the class since you're already inside the class.

How to define Ruby Test::Unit testcase with `must`

I had a test case of the following form:
require 'test/unit'
class SomeTests < Test::Unit::TestCase
def test_should_do_action
assert true
end
end
and have re-written it using must as suggested in the book A Test::Unit Trick to Know About:
require 'test/unit'
class SomeTests < Test::Unit::TestCase
must "do action" do
assert true
end
end
And when I run it, I get an undefined method 'must' error shown as follows:
SomeTests.rb:3:in `<class:SomeTests>': undefined method `must' for SomeTests:Class (NoMethodError) from
SomeTests.rb:2:in `<top (required)>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:10:in `block (2 levels) in <main>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:9:in `each' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:9:in `block in <main>' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:4:in `select' from
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb:4:in `<main>' rake aborted! Command failed with status (1): [ruby -w -I"lib" -I"/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0" "/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rake/rake_test_loader.rb" "test/**/*Tests.rb" ]
Tasks: TOP => default => test (See full trace by running task with --trace)
I thought that must might be a part of minitest, so I required 'minitest/unit' instead, but I still get an error. I also assume that must keyword isn't part of rspec, which I'm not using yet.
How do I get this to work properly?
It looks like that method is not provided out of the box, but was developed by a third party. You need to add code described here.

Rails 4: Undefined method on module

I have a module in app/misc/dsl/builder.rb that has this code
module Dsl
class Builder
def initialize(context, &block)
return if not block_given?
parent_context = block.binding.eval "self"
parent_context.extend Proxy
parent_context.object = context
parent_context.instance_eval &block
end
end
def self.set_context(context, &block)
Dsl::Builder.new(context, &block)
end
end
Note: this directory misc is preloaded in application.rb
config.autoload_paths += Dir[Rails.root.join('app', 'models', '{**/}'),
Rails.root.join('app', 'misc', '{**/}')
]
Then, somewhere in the text (lets say at foo.rb) I have this code:
Dsl.set_context(obj) do
#some code with obj receiving messages
end
The test stack we are using consists on Zeus+Guard+Rspec. Now, lets say I rewrite the code to something not working
Dsl.set_context(obj) do
asdqwe #this message does not exists
end
From times to times, I receive this baffling message
1) SomeOtherClass search_hash receiving keywords params should query for those keywords
Failure/Error: subject.search_hash
NoMethodError:
undefined method `set_context' for Dsl:Module
# ./app/misc/product_query.rb:116:in `base_search_hash'
# ./app/misc/product_query.rb:25:in `search_hash'
# ./spec/misc/product_query_spec.rb:78:in `block (4 levels) in <top (required)>'
# -e:1:in `<main>'
instead of the correct message that should be regarding undefined method asdqwe
Any clue about this?
Look here
it says:
Rails 3 has been updated such that classes/modules (henceforth, C/M)
are lazy loaded from the autoload paths as they are needed
so, you can do require_relative 'app/misc/dsl/builder.rb' in your rspec_helper.rb (can it be better with just require?) The problem must be that the loader doesn't know in advance where to find Dsl.set_context, but he will know once you have referenced Dsl::Builder
Hope it helps

Sinatra, Mongoid, Heroku, MongoHQ: connecting to Mongodb

Trying to get Mongoid up and running with Sinatra on Heroku (MongoHQ). Previous experience with Rails but first time with the stack and Sinatra.
Started with one of the simple examples on the web (app.rb):
require 'rubygems'
require 'sinatra'
require 'mongo'
require 'mongoid'
configure do
Mongoid.load!('mongoid.yml')
Mongoid.configure do |config|
if ENV['MONGOHQ_URL']
conn = Mongo::Connection.from_uri(ENV['MONGOHQ_URL'])
uri = URI.parse(ENV['MONGOHQ_URL'])
# problem happens here
config.master = conn.db(uri.path.gsub(/^\//, ''))
else
config.master = Mongo::Connection.from_uri("mongodb://localhost:27017").db('test')
end
end
end
# Models
class Counter
include Mongoid::Document
field :count, :type => Integer
def self.increment
c = first || new({:count => 0})
c.inc(:count, 1)
c.save
c.count
end
end
# Controllers
get '/' do
"Hello visitor n" + Counter.increment.to_s
end
For reference, mongoid.yml looks like:
development:
sessions:
default:
database: localhost
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
As per app.rb (# problem happens here), my logs say:
/app/app.rb:15:in `block (2 levels) in <top (required)>': undefined method `master=' for Mongoid::Config:Module (NoMethodError)
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.3/lib/mongoid.rb:112:in `configure'
from /app/app.rb:11:in `block in <top (required)>'
from /app/vendor/bundle/ruby/1.9.1/gems/sinatra-1.3.2/lib/sinatra/base.rb:1273:in `configure'
from /app/app.rb:8:in `<top (required)>'
I have also tried variants, including:
config.master = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXX')
Mongoid.database = Mongo::Connection.from_uri(ENV['MONGOHQ_URL']).db('appXXXXXXX')
But get the same error:
undefined method `master` for Mongoid::Config:Module (NoMethodError)
or:
undefined method `database=` for Mongoid::Config:Module (NoMethodError)
What am I missing?
Shouldn't be
configure do
Mongoid.load!('mongoid.yml')
end
enough?
That's what the mongid docs are saying. The MONGOHQ_URL environment variable already contains every information to initialize the connection to the db.
So was using Mongoid 3.x ... which:
Doesn't use 10gen driver: uses Moped
Doesn't use config.master
The canonical sample code above which is all over the web works out of the box with Mongoid 2.x so have dropped back to that for the time being.
Thanks!

Resources