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...
- Run
rails db:drop
- Delete
db/schema.rb
file. - Delete
db/migrate/20190915133638_add_devise_to_users.rb
- Run
rails generate devise:install
again - 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:
rails g model User
- Change the name of the migration file so it will be executed right before the
AddDeviseToUsers
migration. - 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
Trying to Set Up Amazon's S3 Bucket: 403 Forbidden Error & Setting Permissions
Clicking Link with JavaScript in Mechanize
How to Share State Between Scenarios Using Cucumber
How to Split a String into Consecutive Substrings of Length at Most 3 in All Possible Ways
Trying to Compare Two Text Files, and Create a Third Based on Information
Rake Aborted! Stack Level Too Deep
Undefined Method Respond_To in Rails 5 Controller
I = True and False in Ruby Is True
Install Ruby 2.2 on MAC Osx Catalina with Ruby-Install
How to Run "Bundle Exec Jekyll New ."
In Ruby How to Automatically Populate Instance Variables Somehow in the Initialize Method
Is There an Equivalent of Array#Find_Index for the Last Index in Ruby
Ruby - Test Whether Database Connection Is Possible
Is There a Pry Debug Setup That Works with Ruby 2.0
Listing Directories at a Given Level in Amazon S3
How to Remove Xcode 4.2 and Install 4.1 to Develop Ruby/Rails on Osx Lion