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.
Related
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
I'm trying to prevent rails from exposing the devise_token_auth registration route so that admins can only be created from the console.
My admin.rb looks like this:
class Admin < ActiveRecord::Base
devise :database_authenticatable, :confirmable,
:recoverable, :trackable, :validatable,
:omniauthable
include DeviseTokenAuth::Concerns::User
end
I'm not sure what I should put in my config/routes.rb to prevent rails from exposing the route.
Removing :registerable ,:omniauthable and :confirmable from the model should do the trick.
Try adding this to your routes as well:
mount_devise_token_auth for 'Admin', at: 'admin_auth', :skip => [:registrations]
I'm trying to figure out how to create a new profile for the user that has just been created,
I'm using devise on the User model, and the User model has a one to one relationship with the UserProfile model.
Here's what my User Model looks like:
class User < ActiveRecord::Base
has_and_belongs_to_many :roles
belongs_to :batch
has_one :user_profile
after_create :create_user_profile
def create_user_profile
self.user_profile.new(:name => 'username')
end
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
This generate the following error:
undefined method `new' for nil:NilClass
i've tried User.find(1).user_profile in rails c and that works so I'm pritty sure the relationship is setup correctly,
I'm probably being a big fat noob and trying to fetch self incorrectly.
plus can you also tell me how to access the params in a Model... is that even possible?
A has_one relationship will automatically add the :create_<relationship> and build_<relationship> methods. Checkout the rails guide.
You could try this:
after_create do
create_user_profile(:name => 'username')
end
Or more likely
after_create do
create_user_profile(:name => self.username)
end
I am trying to add a role to a specific user in my Rails Console via Heroku , but I am getting the error:
NoMethodError: undefined method `role=' for # .
If you take a look at the screenshot below the role you can see all of the columns in the User table that is available:
The command I am using from the rails console is below to assign the role to the user. I saw a similar post on StackOverflow about the same error but after the db migrate I still got the same error. Can someone point me in the right direction possibly?
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable
has_many :posts
def admin?
role == 'admin'
end
def moderator?
role == 'moderator'
end
end
I am trying to get a user who has not filled out their profile to redirect to the edit page.
I have two tables - Users (Devise handles) and Profiles.
Profiles has a row called profile_complete
In my user model I am trying to define a method called profile_complete which takes the boolean of profiles_complete and passes it back.
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
has_many :profiles, :dependent => :destroy
def profile_complete?
self.profile.profile_complete == true
end
end
But cannot seem to figure out what the line in the profile_complete method is. I have got the other bits working in the correct place but cant get this variable across Any help? Cheers :)
def profile_complete?
self.attributes.values.include?(nil) ? false : true
end