Rake db:migrate fails on devise User using mysql db - ruby-on-rails

So i am following the process based on Devise read.me Getting Started. This is the process i did
Created my rails project
Added gem 'devise'
rake db:create
rails generate devise:install
Added home#index to routes
added action_mailer_deafult_url_options to development.rb
rails generate devise User
rake db:migrate when i run rake db:migrate, it fails and gives me this error report
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
super: no superclass method sanitize_for_mass_assignment' for #<ActiveRecord::SchemaMigration version: nil> NoMethodError: super: no superclass methodsanitize_for_mass_assignment' for #
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :validatable, :registerable
end
migration file
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.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, 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
Does anyone know any reason why this is happening
I am using rails - 4.2.3 and Devise 3.5.2

If you are facing this issues and you have installed gem 'strong_parameters' please remove it because it has been added in ActiveModel and ActiveController since Rails 4.0. Documentation got me confused so i added the gem and tht caused my error.

Related

Rails devise isn't saving the password upon registration

I am using devise 4.8.1 with rails 7.0.3 and postgresql, I just started using devise, and I generated the views using rails g devise:views and then applied the migration using "rails db:migrate"
This is my user model:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable, :confirmable
has_many :posts, foreign_key: 'author_id'
has_many :comments, foreign_key: 'author_id'
has_many :likes, foreign_key: 'author_id'
attr_accessor :password, :password_confirmation
validates :name, presence: true
validates :PostsCounter, presence: true, numericality: { greater_than_or_equal_to: 0 }
def recent_posts
posts.order(created_at: :desc).limit(3)
end
end
This is the migration that I added:
# frozen_string_literal: true
class AddDeviseToUsers < ActiveRecord::Migration[7.0]
def self.up
change_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.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, 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
# Uncomment below if timestamps were not included in your original model.
# 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
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
This morning devise was working fine, when I sign up the password gets encrypted and then saved now when I sign up and then try to login it says invalid email/password, when I checked my PostgreSQL database I found out that nothing is getting saved in the encrypted_password column as shown in this picture, I tried searching online but couldn't find any solution, I also tried to reinstall the gems and nothing worked, I am not sure how to fix this issue, please assist me with this.
If you need any more information about my code please let me know.
Edit: I am not sure if it's okay to share this but this is my GitHub repository and branch that has the issue (the issue is only presented in that branch feature/devise), I tried to share some pieces of the code I have but it's better to see the project structure in my opinion.
Did you try creating user from the rails console? If it gets saved with encrypted password, there is nothing wrong in the code. If it does not, it will throw the error and you can debug from there?

Devise not working in production due to nil to_sym method

I have been trying to add user login / authentication to my RoR site. I am using Devise.
Things work fine in my dev instance (I use Cloud9, in case useful).
However when I push to Heroku / production, for any page related to the user functionality (e.g. User Sign up, User login, or a page which checks if the user is logged in), it errors out. All Heroku gives me is:
NoMethodError (undefined method `to_sym' for nil:NilClass):
Just looking for some ideas on what could be happening?
user.rb:
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
enum role: [:user, :business, :admin]
after_initialize :set_default_role, :if => :new_record?
def set_default_role
self.role ||= :user
end
DeviseCreateUsers migration:
# frozen_string_literal: true
class DeviseCreateUsers < ActiveRecord::Migration[5.0]
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.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, 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.integer :role, default: 0
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
Many thanks!

Unable to integrate lockable for number of failed attempts into Rails app

I am new to rails+angular. For my app, I want to lock out user after 5 failed password attempts. I decided to follow this:
How to make Devise lockable with number of failed attempts
But when I try logging in after 5 failed attempts, it lets me through
I added the :locked module to my user.rb (shown below) file so that the locked feature appears in devise.rb
User.rb
class User < ActiveRecord::Base
include TokenAuthenticatable
TYPE = {
:admin => 1,
:member => 2
}
devise :database_authenticatable, :registerable, :recoverable,
:rememberable, :trackable, :validatable, :token_authenticatable,
:timeoutable, :lockable ##this was added
has_and_belongs_to_many :clients
end
In my migrations then, I un-commented out the lockable part of it :
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
t.string :first_name
t.string :last_name
t.string :type
## 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
t.timestamps
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
# add_index :users, :authentication_token, :unique => true
end
end
To troubleshoot this, I also tried adding :locked, :failed_attempts => 5and uncommented out all the lockable features in initializer/devise. I am unsure on how to proceed - help would be appreciated.

Devise Signup button not working in rails

I have a rails app in which I am using devise gem for user signup and login. I followed all the steps to use devise:
added following to gemfile
gem 'devise'
then
bundle install
After executing
rails g devise:install
I ran the following command
rails g devise user
After this I did
rake db:migrate
Everything goes well, devise gem got installed migration was also successful and the url
http://localhost:3000/users/sign_up
shows the signup form. But after filling the data on clicking signup button nothing is happening. No error on console. No data is being inserted to the table. Any help would be appreciable.
console log
Started GET "/users/sign_up" for 127.0.0.1 at 2015-06-06 15:04:56 +0530
ActiveRecord::SchemaMigration Load (0.3ms) SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by Devise::RegistrationsController#new as HTML
Rendered /home/ajeet/.rvm/gems/ruby-2.2.0/gems/devise-3.4.1/app/views/devise/shared/_links.html.erb (5.5ms)
Rendered /home/ajeet/.rvm/gems/ruby-2.2.0/gems/devise-3.4.1/app/views/devise/registrations/new.html.erb within layouts/application (28.3ms)
Completed 200 OK in 278ms (Views: 257.9ms | ActiveRecord: 1.0ms)
User devise migration file content
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.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, 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
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
Database is mysql
content of routes.rb file
devise_for :users
root 'home#index'
get 'home/index'
In your User Model add :confirmable and set your root to: in your routes.rb.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
Your routes.rb should look like something like this:
root to: 'home#index'
I forgot to add, please uncomment these lines:
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
You can use Heroku to get a free SendGrid account for e-mail activation.
Bottom of your dev environment:
config.action_mailer.default_url_options = { host: 'localhost:3000' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true

Devise: undefined method `params_authenticatable?' for User:Class

I am using devise for authentication in my rails app.
I keep getting different errors like "undefined method XXXX for User:class".
Looking for some help to setup devise correctly.
I have the module database_authenticable setup for Users and I hit the error:
undefined method `params_authenticatable?' for User:Class
Its comming from the following devise code in lib/devise/strategies/authenticatable.rb
def params_authenticatable?
mapping.to.params_authenticatable?(authenticatable_name)
end
Here are my devise settings:
class User < ActiveRecord::Base
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :trackable, :validatable , :omniauthable, :omniauth_providers => [:linkedin]
.
.
end
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
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
t.timestamps
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
# add_index :users, :authentication_token, :unique => true
end
end
Mu authentication requirements are straightforward and I want to use Linkedin authentication and eventually allowing other omni auths as well.
I am wondering if I should use socery or something else for simple authentication
Thanks !
I am using Rails4.0

Resources