Obstacle with Ruby Rails Authentication - ruby-on-rails

I am a brand new Ruby on Rails User
When I enter localhost:3000 ,
I get an error reading
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=development
You have 2 pending migrations:
20221119205559_add_devise_to_users.rb
20221119211811_drop_users_table.rb
# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending.
def check_pending!(connection = Base.connection)
raise ActiveRecord::PendingMigrationError if connection.migration_context.needs_migration?
end
def load_schema_if_pending!
In a nutshell, it was working before but I attempted to create a a basic authentication page(which was also working) , but for some reason when I clicked sign up I received an error also.
Thank you for any tips on how to fix this!
I have tried to
rake db:drop
rake db:create
rake db:migrate
Editing the migrate file with:
edit your migration file
class DropUsersTable < ActiveRecord::Migration
def change
drop_table :users
end
end
Then doing rake db:migrate
Also, I have run :
rails generate devise:install
rails generate devise User
bundle exec rake db:migrate

Related

Devise token auth preventing migration

My rails app is using Devise. I added devise_token_auth so I can link the app to an android app.
In the routes
namespace :api do
scope :v1 do
#mount_devise_token_auth_for 'User', at: 'auth'
end
end
in the initializer
# enable_standard_devise_support = false #for working with Devise
(in addition the other code that was in the file)
I got this error
:~/workspace (master) $ rake db:migrate
-- [](4.2)
-- [](4.2)
rake aborted!
NoMethodError: undefined method `[]' for #<ActiveRecord::Migration:0x00000002c3c470>
Solution, I deleted modified the migration file
class DeviseTokenAuthCreateUsers < ActiveRecord::Migration[4.2]
to
class DeviseTokenAuthCreateUsers < ActiveRecord::Migration
I stopped getting the error but now I got this
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id" IN ........
Please help, how can I get this to work?
Try the following steps :
rake db:drop
rake db:create
rake db:migrate

raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?

def check_pending!(connection = Base.connection)
raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?(connection)
end
def load_schema_if_pending!
i have run: bin/rake db:migrate RAILS_ENV=development but still nothing happens, i was trying to add community engine to my rails 4.2 app, help anyone?
Thanks
Per the installation instructions you need to run rake community_engine:install:migrations before rake db:migrate.

How to undo the action of rails db:migrate in Rails 5.0.0.1

When migrating the database, I made a spelling mistake.
I want to generate a scaffold by running:
rails generate scaffold Micropost context:text user_id:integer
rails db:migrate
Although I made a mistake by leaving out the colon when I ran:
rails generate scaffold Micropost context:text user_id integer
rails db:migrate
I want to undo this migration, how to do it?
(I'm using Rails 5.0.0.1)
When I run rails db:migrate, I get an error of:
SQLite3::SQLException: table "microposts" already exists:
When I run rails db:migrate:status, I get the following output:
Status Migration ID Migration Name
up 20161024021157 Create users
up 20161024025545 ********** NO FILE **********
down 20161024025805 Create microposts
I tried to use rails db:migrate:down VERSION=20161024025805. There wasn't any message showing in the command line. Then I ran rails db:migrate. The error is the same.
rails db:rollback will simply rollback one migration which I believe is what you are looking for
For a more specific rollback, you can run rails db:migrate:down VERSION=numberofversion
Replace the numberofversion with the version number of the migration file generated, for example:
rails db:migrate:down VERSION=1843652238
Edit:
Since you are getting the error that the Microposts table already exists, you must follow these steps to remove the table:
Run rails generate migration DropMicroposts
Go to the /db/migrate folder and find the latest migration file you just created
In that file paste this:
class DropMicroposts < ActiveRecord::Migration
def up
drop_table :microposts
end
end
run rails db:migrate
After this, run rails generate scaffold Micropost context:text user_id:integer
Then run rails db:migrate

Rails 4 Migration Error (Can't rake db:migrate) SQLite3::SQLException: duplicate column name

I've been trying to get the Paperclip gem working. The problem that I was initially running into was that pictures were getting uploaded but not displaying. I then messed around with the database by doing a rake db:rollback to try and fix the error. Now I can't rake db:migrate again because of this error
SQLite3::SQLException: duplicate column name: image_file_name: ALTER TABLE "posts" ADD "image_file_name" varchar
I've personally went into the migration folder and deleted the file to try and generate a migration again. I've been trying to do rails generate paperclip post image and it does create a migration file, but I'm unable to rake db:migrate.
Any suggestions?
Thanks!
Deleting a migration file doesn't really rollback the change it made in your database. Your best bet is to:
Don't delete the migration but comment out the content of the migration class, and run rake db:migrate,
Let's say I have this as my migration file
class AddEmailSentToNeeds < ActiveRecord::Migration
def change
add_column :needs, :email_sent, :boolean ,default: false
end
end
Just comment out the method but leave the class, so it would be:
class AddEmailSentToNeeds < ActiveRecord::Migration
# def change
# add_column :needs, :email_sent, :boolean ,default: false
# end
end
This is just a hacky way to tell rails to skip this migration.
OR
start from the start so go do, rake db:drop, rake db:create, and rake db:migrate

Test fails when adding ":remember_token"

I'm following chapter 8 of Michale Hartl's tutorial. When I add:
it { should respond_to(:remember_token) }
to the user_spec.rb file the test fails, with the failure notice pointing directly to this one line in the file.
Prior to adding this line, all tests past.
The only other steps I took after adding this line (and before running the test) was to run a remember token:
$ rails generate migration add_remember_token_to_users
I then updated db/migrate/[timestamp]_add_remember_token_to_users.rb as follows:
class AddRememberTokenToUsers < ActiveRecord::Migration
def change
add_column :users, :remember_token, :string
add_index :users, :remember_token
end
end
and development and test databases as usual:
$ bundle exec rake db:migrate
$ bundle exec rake db:test:prepare
As Spork caches the rails environment it does not know about the migration until it is restarted.
Therefore you need to restart the Spork server so that it will reload the rails environment including the new migrations.

Resources