Devise, where to add a model - ruby-on-rails

I want to add a timeoutable model to devise after my initial configuration.
I enabled the config/initializers/devise.rb:
config.timeout_in = 30.minute
But where do I actually add the devise :timeoutable model?

After you install devise you need to configure model using built in generator
rails generate devise MODEL
example rails generate devise User. See this section
https://github.com/plataformatec/devise#user-content-getting-started
Once you have that inside app/models/user.rb you can add desired modules
class User < ApplicationRecord
devise :timeoutable, :database_authenticatable, :registerable,:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :confirmable
end
Hope it helps

Related

CanĀ“t change password in Administrate or Madmin. Rails 7 + Devise

Try installing administrate(thoughtbot) and Madmin(GoRails). But in both cases when I change the password of a user in /admin or /madmin and try to login again with the new password, I get the following error.
raise Errors::InvalidHash.new("invalid hash")
I check that user.rb has :database_authenticatable
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
end
Is just standar rails new 7 + devise, bcrypt is commented
# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"
The users can edit his own password but i need to close the new signups and only create new users manually in /admin

`method_missing': undefined method `devise_modules'

I'm trying to separate models from an RoR app into a gem. I'm getting an error when I extended the User model from the gem for adding Devise instance methods
I've tried different methods
User.class_eval and ModelsGem::User.class_eval
Single table inheritance like: class SuperClass < ModelsGem::User; end
Overriding the model class like class User < ActiveModel::Base
None of them worked with devise.. However, I could access methods of User model from the gem in the app and everything is working as expected other than devise.
You can do something like this
rails g devise:views
rails g devise user
if we wanna to add sth like first name and last name put it in the db before rake db:migrate
t.string :first_name
t.string :last_name
then rake db:migrate
in the user model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable, :confirmable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
end
I hope I did solve your problem

NameError in RailsAdmin::Main#index

I'm using rails_admin along with the devise gem, looks like everything is working fine with dashboard but whenever I switch to user it throw uninitialized constant User::Authentication raise NameError. new("uninitialized constant #{candidates.first}", candidates.first) error. https://i.stack.imgur.com/DWOP4.png This is my error screenshot. Any help will be appreciated.
Below is my user.rb file,
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts
has_many :comments
has_many :authentications
end
I struggled with this today while trying to namespace a model, and found that the basic search filters were to blame;
The problem was resolved as soon as I added a new filter; I'd say it's because adding a new filter removed the basic filters that created the issue... (here is an explanation on how to do it : https://activeadmin.info/3-index-pages.html)
I hope this will be useful.

How to Create a User Profile in Rails?

I am using Devise for user registration and authentication but want to have user profiles too, for example /user/john-doe with all the fields like Company Name, Phone Number etc which the person can add.
Is there any Rails way to get it done out of the box? any gem for this? if not, can someone give me some direction on how to do it?
There is no gem to add company name, phone etc (at least I know :)), but normally what I do is,
I add the above columns to table, via a migration
Ex: add_column :users, :company_name, :string
then I update the model accordingly
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, *:company_name*
end
then I run this command to copy the devise views
rails generate devise:views
then I update the corresponding view with new text_boxes etc..
HTH

Devise find_for_authentication not firing with multiple authentication keys

I have two devise models: User and Member
As such, I'm specifying authentication keys on the models themselves instead of in the Devise initializer.
Member.rb
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :authentication_keys => [:email, :subdomain]
I also override
def self.find_for_authentication(conditions={})
debugger
conditions[:account_id] = Account.find_by_subdomain(conditions.delete(:subdomain)).id
super(conditions)
end
Unfortunately, when authentication_keys has multiple keys, my find_for_authentication method doesn't appear to be firing. Works fine when I specify just one key. Any thoughts?
subdomain should have been a request_key since it's part of the request.
request_keys => [:subdomain]

Resources