我最近刚将我的应用程序部署到Heroku但是当我运行Heroku时运行rake db:migrate我得到以下错误。
NameError: uninitialized constant User
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `const_get'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:261:in `block in constantize'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `inject'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/inflector/methods.rb:259:in `constantize'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise.rb:292:in `get'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/mapping.rb:81:in `to'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/mapping.rb:76:in `modules'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/mapping.rb:93:in `routes'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/mapping.rb:160:in `default_used_route'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/mapping.rb:70:in `initialize'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise.rb:326:in `new'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise.rb:326:in `add_mapping'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/rails/routes.rb:238:in `block in devise_for'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/rails/routes.rb:237:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/devise-3.5.6/lib/devise/rails/routes.rb:237:in `devise_for'
/app/config/routes.rb:4:in `block in <top (required)>'
/app/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:434:in `instance_exec'
/app/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:434:in `eval_block'
/app/vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.5/lib/action_dispatch/routing/route_set.rb:412:in `draw'
/app/config/routes.rb:1:in `<top (required)>'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:40:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:40:in `load_paths'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:16:in `reload!'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:26:in `block in updater'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/file_update_checker.rb:75:in `call'
/app/vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.5/lib/active_support/file_update_checker.rb:75:in `execute'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:27:in `updater'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application/finisher.rb:69:in `block in <module:Finisher>'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `instance_exec'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:30:in `run'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:55:in `block in run_initializers'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/initializable.rb:54:in `run_initializers'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application.rb:352:in `initialize!'
/app/config/environment.rb:5:in `<top (required)>'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application.rb:328:in `require_environment!'
/app/vendor/bundle/ruby/2.2.0/gems/railties-4.2.5/lib/rails/application.rb:457:in `block in run_tasks_blocks'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
以下是我的设计创建用户表(20160403200237_devise_create_users.rb)
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, default: 0, null: false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.inet :current_sign_in_ip
t.inet :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end
以下是一个名为(20160403200704_add_email_to_users.rb)
的新增列class AddEmailToUsers < ActiveRecord::Migration
def change
add_column :users, :playlist, :text
end
end
以下是用户模型(user.rb)
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
serialize :playlist, Hash
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
只有当我在heroku中运行rake db:migrate时才会出现这些错误。当我在本地运行时,即使使用rails s -e生产生产模式,事情也会顺利进行。我真的可以在这个上使用帮助。谢谢!
以下是我的Gemfile
source 'https://rubygems.org'
gem 'figaro'
gem 'bootstrap-sass'
gem 'font-awesome-sass'
gem 'devise'
gem 'rails_12factor', group: :production
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.2.5'
# 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'
# 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'
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
答案 0 :(得分:0)
首先在gem pg
gem rails_12factor
和:production group
group :production do
gem 'pg'
gem 'rails_12factor'
end
在本地运行以更新gemlock文件bundle install
。
然后推送到git push heroku master
。
链接:PG RESET
要删除数据库,如果您使用的是SHARED_DATABASE_URL:
在heroku pg:reset DATABASE
pg:reset
之后
! WARNING: Destructive Action
! This command will affect the app: NAME OF YOUR HEROKU APP
! To proceed, type "NAME OF YOUR HEROKU APP" or re-run this command with --confirm NAME OF YOUR HEROKU APP
要重新创建数据库中没有任何内容:
heroku run rake db:migrate
您可以通过执行以下操作将最后两个合并为一个操作:
heroku run rake db:setup