Devise Install from Existing Model/Database

Devise install from existing model/database

It looks like that documentation is outdated.

Try using devise generator, it will create same migration, with correct parameters, it's ok if its an existing model:

rails g devise customer

it should create AddDeviseToCustomers migration

with something similar to this:

class AddDeviseToCustomers < ActiveRecord::Migration
def self.up
change_table(:customers) 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
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :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 # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at

## Token authenticatable
# t.string :authentication_token

# Uncomment below if timestamps were not included in your original model.
# t.timestamps
end

def self.down
# By default, we don't want to make any assumption about how to roll back a migration when your
# model already existed. Please edit below which fields you would like to remove in this migration.
raise ActiveRecord::IrreversibleMigration
end
end

note that there is no more t.confirmable

Step on how to add devise migration to existing User model in ruby on rails?

Just add devise_for :user in your routes

Add attr_accessible :password, :password_confirmation

and for more info take a look at a typical devise model

https://github.com/johndel/Rails-Simple-CMS/blob/master/app/models/admin.rb

(Pretty simple)

Problems while installing devise

Run rails g devise:install.

It will generate config/initializers/devise.rb file with all these settings.

How do I add Devise's 'timeoutable' module to an existing Devise install? - Rails 3.1

timeoutable refers to the login session timeout. No extra columns are needed, just add it to your model.

The timeoutable hook contains all the magic (source: https://github.com/plataformatec/devise/blob/master/lib/devise/hooks/timeoutable.rb)

Installing Devise: `PG::UndefinedTable: ERROR: relation users does not exist`

Did you have a User model before? It seems that devise thought that too and it's trying to add its columns to a non-existent User model.

This is what I would do...

  1. Run rails db:drop
  2. Delete db/schema.rb file.
  3. Delete db/migrate/20190915133638_add_devise_to_users.rb
  4. Run rails generate devise:install again
  5. Run rails db:migrate

EDIT

If the previous steps didn't work then try to create a users table before devise's migration.

For this follow these steps:

  1. rails g model User
  2. Change the name of the migration file so it will be executed right before the AddDeviseToUsers migration.
  3. Run rails db:migrate

Typus with existing devise model getting 401 unauthorized error

You are adding :confirmable in your account model when you signup it will send you the confirmation mail to confirm your email, if you login without confirming it will throw an Unauthorized error.

Error when using gem 'devise' for an already existing model

Block in change_table add columns to existed table users. In change_table you trying to add column email, but this column already exists in table users. This column was created in your first migration.

You should comment follow string in devise migration:

 t.string :email,              null: false, default: ""

And add follow string in devise migration after change_table (NOT IN BLOCK) method:

change_column :users, :email, :string, null: false, default: ""

Once I have devise installed, how do I add (and remove) modules easily? - Rails 3

Update: Here's Devise wiki page on how to change existing table to suit Devise needs:

For Devise below 2.0

https://github.com/plataformatec/devise/wiki/How-To:-change-an-already-existing-table-to-add-devise-required-columns

For Devise after 2.0

https://github.com/plataformatec/devise/wiki/How-To:-Upgrade-to-Devise-2.0-migration-schema-style

How to add products to the Devise user model

I am a bit confused about the actual problem you are having, but if your question is about links to your newly created Product... Your routes file defines products as:

resources 'products'

This will create a set of routes that have the base form:

<root>/product/:id

So, the URI namespace for your products is not currently scoped by user, and all you need is a product ID. So, if you have want to include a link to a user's set of products, you could do something like:

<% current_user.products.each do |product|%>
<%= link_to product.name, product_path(product.id) %>
<% end %>

If you really want the paths to products to be scoped by each user, you can do that too. The routes file allows you to nest resources inside another like this:

resource :users do
resources :products
end

That will generate routes of the form:

<root>/users/:user_id/products/:id

I suggest checking out the Rails Guide page on routes ( https://guides.rubyonrails.org/routing.html) to get an idea of the various things you can do to set up routes the way you want them.



Related Topics



Leave a reply



Submit