Undefined method in Rails console when adding role to user - ruby-on-rails

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

Related

attr_encrypted?': undefined method `has_key?' for nil:NilClass (NoMethodError)

I am working on Multi Factor authentication on a rails api with devise JWT auth.
I am following the tutorial that comes with the gem "devise-two-factor", here is the link to it : https://github.com/tinfoil/devise-two-factor
When I add an encryption key in the model as an environment variable and run this command :
rails generate devise_two_factor MODEL ENVIRONMENT_VARIABLE
I get this error that I couldn't find anything relevant to :
`attr_encrypted?': undefined method `has_key?' for nil:NilClass (NoMethodError)
Here is the 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,
:jwt_authenticatable,
jwt_revocation_strategy: JwtDenylist
devise :two_factor_authenticatable,
:otw_secret_encryption_key => ENV['encryption_key']
end
I can't understand the problem because I'm not experienced with rails and not a good Ruby developer

`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.

Rails Trying to create a profile after the user has been created

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

undefined method `current_user' ( commontator + devise )

I am using devise & commontator gems
i don't know what the error about "current_user" when try to "show" the product !!
undefined method `current_user' for #<ProductsController:0xd09600c>
user model
class RegisteredUser < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Registered user can comment on a product
acts_as_commontator
end
product model
class Product < ActiveRecord::Base
# Product can be commented on
acts_as_commontable
end
i call show method from ProductsController
class ProductsController < ApplicationController
def show
commontator_thread_show(#product)
end
end
the devise helper based on model name
so it is current_registered_user not current_user
have to change it in commontator initializer file then restart the server

Resources