rubysinatrasinatra-activerecord

Ruby: NameError at '/signup' uninitialized constant User


I'm new to Ruby and working on creating a form that creates a new user when submitted. I have created my database (rake: 12.0.0) and am able to edit it when using tux (so I am assuming that my model files are correct). I am using Sinatra to launch the website (which works), but when I submit the form, I am receiving this error: NameError at '/signup' uninitialized constant User. Given that I am able to edit the database in tux, I'm assuming that my actions.rb file is not accessing the models files, but I'm not sure why.

ruby: 2.4.1

puma: 3.9.1

sinatra: 2.0.0

tux: 0.3.0

I've looked at other threads with similar topics, but still can't seem to figure out what the problem is. Any help would be great! Thanks

If you need any other information let me know!

This is my models/user.rb file:

class User < ActiveRecord::Base

  validates_uniqueness_of :email, :phone_number

end

actions.rb

require 'sinatra'
require 'rubygems'

set :public_folder, 'public'
post '/signup' do
  [...params -> this section works so I omitted it]

  # Instantiate and save a User
  @user = User.create({firstname: firstname, lastname: lastname, email: email})
end

gemfile

source "https://rubygems.org"

gem 'rake'
gem 'activesupport'

gem 'sinatra'
gem 'sinatra-contrib'
gem 'sinatra-activerecord'

# gem 'puma'
gem 'tux'

group :development, :test do
  gem 'pry'
  gem 'shotgun'
  gem 'sqlite3'
end

Solution

  • You need to require the user.rb file. Try this in your actions.rb file:

    require_relative 'user'