Skip to content

Instantly share code, notes, and snippets.

@jelaniwoods
Last active August 28, 2019 21:29
Show Gist options
  • Save jelaniwoods/5a72e87b123c6295efbc0e4e9970d214 to your computer and use it in GitHub Desktop.
Save jelaniwoods/5a72e87b123c6295efbc0e4e9970d214 to your computer and use it in GitHub Desktop.

Fixing your User table

If you had previously generated your User model with the rails generate draft:devise command here are some steps you should take to resolve

Step 1

navigate to /git and make a commit.

Step 2

In the db/migrate/ folder, look for the file with "create_devise_users" in the name and open it.

devise-create-users

Replace the change method with the following:

def change
  create_table :users do |t|
  t.string :username
  t.string :password_digest
  # ...
  # add any addiional columns for your table here
  # the syntax is t.column_type :column_name
  t.timestamps
  end
end

You can add any other columns you had in your table with between the create_table and the first end keyword.

Step 3

Next, replace the entire content of your user.rb model file with this

class User < ApplicationRecord

end

devise-user-model

Step 4

Now open a new terminal and type

rails db:drop

then hit the enter key to remove all the tables in your database.

Step 5

In the menu dropdown select

rails db:migrate

to recreate your tables, including the update User table.

select-rails-db-migrate

Step 6+

We can follow the same steps we took in photogram-signin from here or copy from the photogram-bootstrap assignment.

  • Add the has_secure_password line to the User model. has-secure-password

  • Create the users_controller.rb and add actions to add the id of the User to the session hash when they log in.

  • Modify the application_controller.rb to force a user to sign in to use your app with a before_action.

  • If you force a User to sign_in with a before_action make sure you exclude the actions you wrote to display the sign in form, the sign up form, the add User id to session action, and the action where you create a User.

    • something like: skip_before_action :authenticate_user!, :only => [:session_form, :add_cookie, :registration_form, :create]
  • Create a current_user helper method in the application_controller.rb.

Last step

Make a git commit so you never lose any of these changes.

If you need a reminder on any step there is the photogram-signin walkthrough video as a reference and we can also be available to walk you through the changes.

@pmckernin
Copy link

Yeah this looks great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment