Uninitialized constant in Rails from Gem - ruby-on-rails

I am fairly new to rails and am chasing my tail trying to get a Rails app to run on my server. Locally on my Mac, everything is working fine, however when I run it on the Ubuntu server, I'm getting a NameError (uninitialized constant Api::V1::TestController::Headless) error. I have updated bundler and the relevant gems as was suggested in other posts. I am certain that Headless is in my gem file, is up to date, and is install properly. I am using Headless in with Watir Webdriver. Any suggestions on what could be causing this error would be greatly appreciated.
Ruby Version: 2.2.3
The controller in question:
class Api::V1::TestController < ApplicationController
respond_to :json
def index
respond_with "test controller"
end
def create
event_submit(params[:json_event])
respond_to do |format|
if ( #log.present? )
format.json { render text: "Log: " + #log }
else
format.json { render text: "Error, no log" }
end
end
end
def nul_check(param)
param ||= "none"
return param
end
def event_submit(params)
#log = ""
#initialize the log
#log = ""
#set the browser that we will use to chrome for testing
#use headless
headless = Headless.new
headless.start
browser = Watir::browser.start 'www.google.com'
#log += browser.title
end
end
The Gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.4'
# Use postgresql as the database for Active Record
gem 'pg'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
#Api gems
gem 'active_model_serializers'
gem 'deathbycaptcha'
gem 'rspec'
gem 'responders', '~> 2.0'
#Driver gems
gem 'watir-extensions-element-screenshot'
gem 'selenium-webdriver'
gem 'chromedriver-helper'
gem 'headless'
gem 'nokogiri'
gem 'watir-webdriver'
group :development, :test do
gem 'sqlite3'
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
gem 'unicorn'

The problem is that Rails looks for the Headless constant under the Api::V1::TestController namespace.
You should use ::Headless (kind of absolute path to constant).

The problem turned out to be server ownership. The user, rails in my case, did not have ownership of the gems since I had run bundle install under my own account. A chmod for the rails user took care of the problem!

Related

How to write an rspec test that interacts with a React form

I'm going to start off by saying I might be going about this the wrong way and I'm open to other ways of testing this.
I have a Rails/React app that I'm trying to add more testing to. I've written rspec tests before that were completely contained to checking things in the backend. Now I'm trying to write a test that checks creating a blog post. I've done this before when the form I was testing was a Rails view, but I've never done this when the form was built in React on the frontend.
This is my gem file:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.7.2'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main'
gem 'rails', '~> 6.1.3'
# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.4', require: false
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
# CSS style
gem "bulma-rails", "~> 0.8.0"
# jquery
gem 'jquery-rails'
# Setup Devise
gem 'devise'
gem 'active_model_serializers'
# Install pry for debugging
gem 'pry'
# Sendgrid/Contact form
gem 'sendgrid-ruby'
gem 'mail_form'
# Env variable management
gem 'figaro', '~> 1.2'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'factory_bot_rails', '~> 6.2'
gem 'rspec-rails', '~> 5.0', '>= 5.0.2'
gem 'faker', '~> 2.18'
gem 'launchy'
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 4.1.0'
# Display performance information such as SQL time and flame graphs for each request in your browser.
# Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md
gem 'rack-mini-profiler', '~> 2.0'
gem 'listen', '~> 3.3'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '~> 3.35', '>= 3.35.3'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
end
This is my test:
require 'rails_helper'
feature 'user signs in', %Q{
As an admin user
I want to create new posts
when I am signed in
} do
scenario 'admin creates new blog post', js: true do
user = FactoryBot.create(:user_1)
post = FactoryBot.create(:post_1)
visit '/post/new'
# binding.pry
fill_in 'title', with: post.title
fill_in 'body', with: post.body
fill_in 'image', with: post.image
fill_in 'keywords', with: post.keywords
binding.pry
click_button 'Submit'
binding.pry
expect(page).to have_content(post.title)
end
end
I've been using binding.pry and save_and_open_page to try and figure out what's going on. The gets to /post/new and fills out the form correctly. So that parts working. But when it clicks on Submit, this error occurs:
[1] pry(#<RSpec::ExampleGroups::UserSignsInAsAnAdminUserIWantToCreateNewPostsWhenIAmSignedIn>)> 2021-08-20 13:51:45 -0400 Rack app ("GET /" - (127.0.0.1)): #<ActionController::UnknownFormat: HomepagesController#index is missing a template for this request format and variant.
request.formats: ["application/json"]
request.variant: []>
I'm not exactly sure what's happening here. I'm guessing I'm missing some part of the config to get the JS aspect of this test to work? Also just to note, this form does work in development/production. The data from the form should be going to the Posts Controller.
Alternative option to getting this to work. Would it make more sense to write response tests for backend stuff. And then just Jest or another library to test frontend functionality?
Your form is submitting, but you do not have the correct format in your Home controller. In the index method, you should add how you want the controller to handle js format.
Can you make sure that your method handles both html and json like this:
respond_to do |format|
format.html # index.html.erb
format.json { render json: #whatever_you_want_to_return }
end
As for testing, your test is good, but you should always test all parts of your application to make sure they return what you intend them to.

Why do I get a missing column error on login?

I'm running spree version 4.0.2 on Rails 6.0.2.1 and I've encountered the following error when I try to login or logout:
ActionView::Template::Error (PG::UndefinedColumn: ERROR: column spree_orders.guest_token does not exist
LINE 1: ...t" IS NULL AND "spree_orders"."currency" = $1 AND "spree_ord...
^
):
10: <ul class="nav navbar-right" data-hook>
11: <li id="link-to-cart" class="nav-item" data-hook>
12: <noscript>
13: <%= link_to_cart %>
14: </noscript>
15:  
16: </li>
I know that the problem is that a query is being generated somewhere when link-to-cart is executed that refers to the column column guest_token which was changed to token in a migration. My Gemfile is shown below.
Can anyone help me with this? I'm relatively new to ruby, rails, and spree, so if I've left out important information, please let me know.
Thanks in advance!
Jeff
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.5'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.0'
# Use sqlite3 as the database for Active Record
gem 'pg'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of web drivers to run system tests with browsers
gem 'webdrivers'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'spree', '~> 4.0'
gem 'spree_auth_devise', '~> 4.0'
gem 'spree_gateway', '~> 3.6'
gem 'aws-sdk-s3', require: false
#gem "google-cloud-storage", "~> 1.11", require: false
gem 'spree_braintree_vzero', github: 'spree-contrib/spree_braintree_vzero'
gem 'spree_i18n', :github => 'spree/spree_i18n'
gem 'spree_globalize', github: 'spree-contrib/spree_globalize'
gem 'activerecord-nulldb-adapter'
For me it seemed to be spree_braintree_vzero gem.
Maybe it doesn't work with the latest Spree v4 ?
How to Uninstall and Remove Spree Braintree VZero
It may be useful to see what the install script is doing here:
https://github.com/spree-contrib/spree_braintree_vzero/blob/master/lib/generators/spree_braintree_vzero/install/install_generator.rb
To uninstall spree_braintree_vzero I had to do the following.
Remove the Gem from Gemfile
gem 'spree_braintree_vzero', github: 'spree-contrib/spree_braintree_vzero'
Remove all.js changes
vendor/assets/javascripts/spree/backend/all.js (git)
//= require spree/backend/spree_braintree_vzero
vendor/assets/javascripts/spree/frontend/all.js
//= require spree/frontend/spree_braintree_vzero
Delete config/schedule.rb
Yep that is all. Unless it existed before you ran the installer.
You can remove it if all it has in it is rake 'spree_braintree_vzero:update_states', otherwise remove that line from it and the surrounding blocks.
Example: https://github.com/spree-contrib/spree_braintree_vzero/blob/1bc400bb598bf177edfe614aef44fcd28eca2312/lib/generators/spree_braintree_vzero/install/install_generator.rb#L15
Add Migrations to Revert Migrations
Note: These could change so check your logs for what you did when you added it.
PS: Don't worry about migrations that touched on the spree_braintree_checkout table since it's going to be dropped.
Protip: don't forget to run your migrations ;)
class DropSpreeBraintreeCheckouts < ActiveRecord::Migration[6.0]
def up
drop_table :spree_braintree_checkouts
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
class RemoveBraintreeIdFromSpreeAddresses < ActiveRecord::Migration[6.0]
def change
remove_column :spree_addresses, :braintree_id
end
end
class RemoveBraintreeTokenAndNonceFromSpreePayments < ActiveRecord::Migration[6.0]
def change
remove_column :spree_payments, :braintree_token
remove_column :spree_payments, :braintree_nonce
end
end

pg::error error relations users already exists

I am trying to deploy my app to heroku. Everything works fine until I try to run "heroku run rake db:migrate". Then I get the following error (please open this image in new tab to see better:
I ran rake db:drop, rake db:create, and rake db:migrate locally. Then I pushed it to heroku and tried running "heroku run rake db:migrate" again, but same error. I know this has something to with the schema already having user table, but I don't see it on my files.
Gem file:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.6'
# Use sqlite3 as the database for Active Record
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# for users
gem 'devise', '~> 4.2'
# Use ActiveModel has_secure_password
gem 'bcrypt', git: 'https://github.com/codahale/bcrypt-ruby.git', :require => 'bcrypt'
# Use Unicorn as the app server
# gem 'unicorn'
gem 'tzinfo-data'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
end
group :test do
# for seeing test covarage
gem 'simplecov', :require => false
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'sqlite3'
end
# upload files
gem 'carrierwave', '~> 1.0'
# bootstraps
gem 'bootstrap-sass', '~> 3.3.4.1'
# Material Icons
gem 'material_icons'
# Material css
gem 'materialize-sass'
group :production do
gem 'pg', '0.15'
gem 'rails_12factor'
end
Is there something else I have to do or am I missing something?
That means your table "users" already exists on heroku. Do you really want to delete all tables on heroku ? If so, you have you can do
heroku pg:reset DATABASE
https://devcenter.heroku.com/articles/heroku-postgresql#pg-reset
If you only want to make some changes on your tables you should really work with migrations like
add_column :users, :my_attribute, ...
removeColumn :users, :my_attribute_to_remove
PS: To drop, create, migrate the db locally will not have any effects at the db on heroku.

Rails 5 : NoMethod Error - Undefined Method Paginate

I have a JSON API based on Rails 5. I have a method that brings list of all the products for one particular company, which I want to paginate so that not to bring up all the data at once. For this I came across two gems, namely will_paginate and api-pagination.
After running bundle install and restarting the server, I'm still getting the following error :
NoMethodError (undefined method `paginate' for #<Api::V1::ProductsController:0x007fb25404db30>)
The Products Controller :
require 'roo'
class Api::V1::ProductsController < ApplicationController
respond_to :json
def index
comp_id = params[:company_id]
cat_id = params[:category_id]
if comp_id
if comp_id && cat_id
product = Product.where(:company_id => comp_id, :category_id => cat_id)
else
product = Product.where(:company_id => comp_id)
end
paginate json: product, status: 200
else
render json: { errors: "Company ID is NULL" }, status: 422
end
end
end
Gemfile.rb
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.2'
# Use mysql as the database for Active Record
gem 'mysql2', '>= 0.3.18', '< 0.5'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
#Devise Gem for Authentication
gem "devise"
#For JSON Serialization
gem 'active_model_serializers'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'carrierwave'
gem 'roo'
#For Pagination
gem 'will_paginate'
gem 'api-pagination'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '~> 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
# Use Capistrano for deployment
gem 'capistrano', group: :development
gem 'capistrano-rails', group: :development
gem 'capistrano3-puma', group: :development
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Can someone tell me where I'm going wrong?
Try to make sure your ApplicationController inherits from ActionController::API instead of ActionController::Base. And if for some reason you can't update your controller, you can include the Rails::Pagination module into your ApplicationController manually.
credits
Update:
class ApplicationController
include Rails::Pagination
......
....
end

Heroku gemfile error when trying to open and edit my site locally

I am building a site using Heroku using a tutorial in my online class. I keep getting this error when attempting to open it locally:
Specified 'sqlite3' for database adapter, but the gem is not loaded. Add `gem 'sqlite3'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
Here is my gemfile:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5'
# Use sqlite3 as the database for Active Record
# gem 'sqlite3'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.1.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0', group: :doc
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Unicorn as the app server
# gem 'unicorn'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'sqlite3'
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
end
group :production do
gem 'rails_12factor'
gem 'pg'
end
This is the full gem::LoadError:
require path_to_adapter
rescue Gem::LoadError => e
raise Gem::LoadError, "Specified '#{spec[:adapter]}' for database adapter, but the gem is not loaded. Add `gem '#{e.name}'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord)."
rescue LoadError => e
raise LoadError, "Could not load '#{path_to_adapter}'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.", e.backtrace
end
Switch to Postgresql. SQLite is not supported.
Try running bundle install locally and then push to Heroku

Resources