undefined method `current_user' ( commontator + devise ) - ruby-on-rails

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

Related

Implementing JWT on Rails

I am having a problem while trying to implement devise jwt.
This is my devise user model:
class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable,
:database_authenticatable,
:jwt_authenticatable,
jwt_revocation_strategy: JwtBlacklist
end
And this is my blacklist.rb model.
class JwtBlacklist < ApplicationRecord
include Devise::JWT::RevocationStrategies::Blacklist
self.table_name = 'jwt_blacklist'
end
This is what i am getting.
Caused by:
NameError: uninitialized constant User::JwtBlacklist
Hope you could help me with this I am new on Rails.
Thank you so much.
Note that include Devise::JWT::RevocationStrategies::Blacklist had been replaced with include Devise::JWT::RevocationStrategies::Denylist according to devise-jwt documentation.
If you're calling your model JwtBlacklist then the filename must be jwt_blacklist.rb so that the auto-loader can find it. Right now the name implies the model is called Blacklist.

`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

Devise Sending Welcome Email

I have a devise model called members i am using devise confirmable. Upon confirm i want to send a welcome email to the User
class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
# Methods
# Override devise confirm! message
def confirm!
welcome_email
super
end
# Private Methods
private
def welcome_email
MemberMailer.welcome_email(self).deliver
end
end
My MemberMailer resides in mailers/brands/member_mailer.rb
class Brands::MemberMailer < ApplicationMailer
# Send Welcome Email once Member confirms the account
def welcome_email(member)
#member = member
mail(to: #member.email, subject: "Welcome to Skreem! Now you Rock!")
end
end
But upon confirming through the mail Link the confirm! is not being overridden and I am not getting any error or email.
Add this to your Member model:
def after_confirmation
welcome_email
end
For more info check after_confirmation
#Pavan thanks for pointing this.
Your welcome_email should be:
def welcome_email
Brands::MemberMailer.welcome_email(self).deliver
end

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 in Rails console when adding role to user

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

Resources