Haven't used Rails in a while and just cloned a project, trying to work with the database but I'm getting this uninitialized constant error and I can't seem to figure out why. I bundle installed, created the database, migrated and seeded some files but I can't seem to find the root of the issue. It's not a small project and it seemed to be working fine back when I used it... Any help would be appreciated it...
Here's my gemfile:
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.1.1'
gem 'httparty'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# 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 CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
# gem 'bcrypt', git: 'https://github.com/codahale/bcrypt-ruby.git', :require => 'bcrypt'
gem 'semantic-ui-sass', git: 'https://github.com/doabit/semantic-ui-sass.git'
# gem 'capistrano-rails', group: :development
gem 'pg_search'
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 'faker'
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', '< 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
gem 'rails_12factor', group: :production
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
And here's the User model:
class User < ApplicationRecord
validates :first_name, :last_name, :presence => true
validates :username, :email, :presence => true, :uniqueness => true
has_many :messages
has_many :posts, :foreign_key => :creator_id
has_many :apprenticeships, :foreign_key => :requestor_id
has_many :skills, through: :posts
has_secure_password
def full_name
"#{self.first_name} #{self.last_name}"
end
end
I have also tried checking if tables were created with ActiveRecord::Base.connection.tables but I'm still getting NameError: uninitialized constant ActiveRecord
Use rails console not irb or pry when working within a Ruby on Rails project. Rails has a complicated bootstrap process, and many of its classes are lazily loaded. Generic interactive consoles like irb or pry do not perform the necessary bootstrap to load a rails app, but rails console will.
Related
I'm trying a query to get the user that created a message in a specific sala but I can't find the way.
I've got the query to get the messages of the specific sala, but I can't get the user that created the message.
Mensajes_controller (where I get the messages from the sala)
def index
#mensaje = #sala.mensajes.desc('_id').limit(20)
render json: #mensaje
end
set_sala (to get the room from which I want the messages)
def set_sala
if params[:sala_id]
#sala = Sala.find(params[:sala_id])
end
end
mensaje model
class Mensaje
include Mongoid::Document
include Mongoid::Timestamps::Created
field :cuerpo, type: String
validates :cuerpo, presence: true
belongs_to :user, foreign_key: :user_id
belongs_to :sala, foreign_key: :sala_id
end
user model
class User
include Mongoid::Document
include ActiveModel::SecurePassword
has_secure_password
field :password_digest, type: String
field :email, type: String
field :nombre, type: String
has_many :salas
has_many :mensajes
validates :nombre, presence: true
validates :email, presence: true
validates :password_digest, presence: true
end
Updating - Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.5.5'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.3'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.12'
gem 'bcrypt', '~> 3.1.7'
gem 'mongoid-bcrypt-ruby'
gem 'devise'
gem 'kaminari-mongoid'
gem 'mongo'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'mongoid', '~> 7.0.5'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
gem 'rack-cors'
gem 'jwt_sessions', '~> 2.3'
gem 'mongoid-rspec', github: 'mongoid/mongoid-rspec', ref: '68c95b133be1a1482fe882e39afd33262147d1f4'
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 'pry-rails'
gem 'rspec-rails'
gem 'factory_bot_rails'
gem 'shoulda-matchers'
end
group :development do
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
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Feel free to ask anything you may need. Thanks!
What about this?
#mensaje.user
and install kaminari-mongoid and maybe mongo gem.
I'm currently trying to deploy my blog using Dokku. I've been following the guides on how to deploy using Dokku, but it wasn't until I got to start deploying the application, I ran into an HTTP Error. I've been using a Mac to develop my blog.
Here's my gem file that I'm using:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.5.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# 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 'mini_racer', platforms: :ruby
gem "loofah", ">= 2.2.3"
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# 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'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
# Use Bulma Rails to simply the CSS part of the site
gem "bulma-rails", "~> 0.7.1"
# Using font-awesome to get icons from font-awesome
gem "font-awesome-rails", '~> 4.7', '>= 4.7.0.4'
gem 'font-awesome-sass', '~> 5.5'
# Use Simple for Authentication
gem 'simple_form', '~> 4.0.0'
# FriendlyID
gem 'friendly_id', '~> 5.1.0'
# ACME-CLIENT
gem 'acme-client', '~> 2.0', '>= 2.0.1'
gem 'nokogiri', '~> 1.8', '>= 1.8.5'
gem 'sassc', '~> 1.12', '>= 1.12.1'
# Use ActiveStorage variant
gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Adds likes to comments and blog posts.
gem 'acts_as_votable', '~> 0.11.1'
#Devise - Authnetication Gem
gem 'devise', '~> 4.4', '>= 4.4.3'
#Pundit - Authorization Gem
gem 'pundit', '~> 1.1'
#CKEditor - To make my textarea and blog posts more robust
gem 'ckeditor', '~> 4.2', '>= 4.2.4', github: 'galetahub/ckeditor'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
gem 'carrierwave', '~> 1.0'
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]
# Call Rspec to test applications
gem 'rspec-rails', '~> 3.7'
#Factory Bot Rails
gem 'factory_bot_rails', '~> 4.7'
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'
# Use the better errors gem to better locate stack trace errors.
gem "better_errors"
#binding-of-caller
gem 'binding_of_caller', '~> 0.8.0'
# Guard is used to simplify running the server
gem 'guard'
# 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'
# Automatically resets the server upon change the view.
gem 'guard-livereload', '~> 2.5', '>= 2.5.2', require: false
gem 'rb-readline'
gem "letter_opener"
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15', '< 4.0'
gem 'selenium-webdriver'
#Database Cleaner - Used to clean the database upon finishing a test.
gem 'database_cleaner'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'pg'
# Require bootstrap
gem 'bootstrap', '~> 4.1.1'
#jQuery for Rails!
gem 'jquery-rails', '~> 4.3', '>= 4.3.3'
#Pagination for the site (Note: "kaminari" is Japanese for lightning.)
gem 'kaminari', '~> 1.1', '>= 1.1.1'
I also found some issue that Bundler 2.0.1 doesn't seem to work. If there's else like a code snippet that you may need to solve the issue, please let me know. Thanks.
Edit: Here's my Application Controller
class ApplicationController < ActionController::Base
include Pundit
rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
added_attrs = [ :username, :email, :password, :password_confirmation ]
devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
devise_parameter_sanitizer.permit :account_update, keys: added_attrs
devise_parameter_sanitizer.permit :sign_in, keys: added_attrs
end
private
def user_not_authorized
flash[:alert] = "You aren't authorized to go to that page. Contact TheBigL through email at master_biglee#live.ca."
redirect_to (request.referrer || root_path)
end
end
Edit: Here's my config/application.rb file
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Blog
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
end
end
I looked at every possible solution regarding this error but nothing solved it.
Every time I try to upload an image, I receive this error:
Unable to autoload constant ActiveStorage::Blob::Analyzable, expected /usr/local/lib/ruby/gems/2.5.0/gems/activestorage-5.2.2/app/models/active_storage/blob/analyzable.rb to define it.
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.5.1'
gem 'rails', '~> 5.2.0'
gem 'mysql2', '>= 0.4.4', '< 0.6.0'
gem 'puma', '~> 3.11'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Reduces boot times through caching; required in config/boot.rb
# Causing crashes. Removing for now
# gem 'bootsnap', '>= 1.1.0', require: false
# Well maintained fork with several patches and compatability for Rails 5.x
# If we need to we can fork this and take ownership of it. Much better than building from scratch.
# gem 'casino', git: "https://github.com/identification-io/CASino.git", branch: "master"
gem 'casino', git: "https://github.com/webappsllc/CASino.git", branch: "master"
# gem 'casino', path: '/Users/justin/dev/oss/CASino'
gem 'grape', '~> 1.1.0' # Pinned
gem 'wisper', '~> 2.0'
gem 'rack-cors', require: 'rack/cors'
# Form Objects
gem "reform", ">= 2.2.0"
gem "reform-rails"
gem "dry-validation"
# Free optimization
gem 'fast_blank'
# Hashids to obscure legacy_ids
gem 'hashids'
# Send Emails via Amazon SES
gem 'aws-sdk-rails'
# Upload Files via Amason S3
gem "aws-sdk-s3", 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]
gem 'guard-rspec', require: false
gem 'dotenv-rails', require: 'dotenv/rails-now'
gem 'spring-commands-rspec'
gem 'parallel_tests', group: [:development, :test]
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'
gem 'grape_on_rails_routes'
gem "better_errors"
gem "binding_of_caller"
gem "letter_opener"
gem 'letter_opener_web', '~> 1.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15', '< 4.0'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
gem 'factory_bot'
gem 'rspec'
gem 'rspec-rails'
gem 'database_cleaner'
gem 'faker'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
# gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
In config/application.rb I have require 'active_storage/engine' uncommented.
In config/environments/development.rb I have config.active_storage.service = :local set up.
I ran rails active_storage:install and rails db:migrate.
The name of the model is user_test
class UserTest < ApplicationRecord
has_one_attached :image
end
I have this in app/controllers/user_tests_controller.rb
def user_test_params
params.require(:user_test).permit(:title, :caption, :image)
end
**config/storage.yml*
test:
service: Disk
root: <%= Rails.root.join("tmp/storage") %>
local:
service: Disk
root: <%= Rails.root.join("storage") %>
I am really clueless as to why it isn't working.
Do you have config.active_storage.service = :amazon set in your development.rb by any chance?
I was getting the above error and it ended up being because I had the above line but the AWS env vars weren't set.
Make sure you check that all of the environment variables present:
AWS access key
AWS secret key
Bucket Name
checking spelling of the environment variables to make sure they match your storage.yml configuration file
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
I followed the docs at https://github.com/thoughtbot/paperclip exactly to install implement paperclip in my app for image uploading. I am currently using gem 'paperclip', '~> 5.0.0.beta1'. After I did the migration, the four columns were added onto my schema properly:
t.string "picture_file_name"
t.string "picture_content_type"
t.integer "picture_file_size"
t.datetime "picture_updated_at"
My paperclip should therefore be installed correctly. However, when I proceeded to add the following two lines onto my model class:
has_attached_file :picture, styles: { medium: "300*300>", thumb: "100*100" }, default_url: "/images/start_project3.jpg"
validates_attachment_content_type :picture, content_type: /\Aimage\/.*\Z/
Everything broke. I try to create, search, or anything related to the model class in rails console, it yells at me with the following error:
NoMethodError: undefined method `has_attached_file' for #<Class:0x0055bd71ec0228>
I have tried multiple versions of paperclip, from the earlier version 4.3.0 to the latest version of paperclip, but the problem persists. I also restarted my server in between changes and migrations, but that did not fix the problem. This is the migration that I performed:
class AddAttachmentPictureToProjects < ActiveRecord::Migration
def self.up
change_table :projects do |t|
t.attachment :picture
end
end
def self.down
remove_attachment :projects, :picture
end
end
I am totally lost right now as to what to do.
This is my gem file:
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.7.1'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.15'
# 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'
# 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'
gem 'pry-rails'
gem 'annotate'
# Use Unicorn as the app server
# gem 'unicorn'
gem 'faker'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'pg_search'
gem 'paperclip', '~> 5.0.0.beta1'
# gem "paperclip", :git => "git://github.com/thoughtbot/paperclip.git"
# gem "paperclip", "~> 4.3"
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug'
gem 'faker'
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 'newrelic_rpm'
gem 'rails_12factor' # error feedback
end
I am not sure what happened. I ended up doing a db roll back and re-migrated the migration yet again. Then I exited all my terminals running in the background and restarted everything. It is working as of now.