I have User model with static method authenticate and I use Rubyoverflow gem.
In the controller I code:
User.authenticate(params[:user], params[:password])
but get the error:
undefined method `authenticate' for
Rubyoverflow::User:Class
How can I say that I need the User from Rails model?
Try
::User.authenticate(params[:user], params[:password])
Related
I am building a rails API and I'm using pundit for authorizations to the API. I am trying to define current_user for pundit with this method:
def pundit_user
User.find_by_other_means
end
I tried implementing it as a private method in my API base controller but it gives me this error:
"exception": "#<NoMethodError: undefined method `find_by_other_means' for #<Class:0x00007f9d25463768>\nDid you mean? find_or_create_by>"
Then I tried implementing it on my application controller and it gives me the following error:
"exception": "#<NameError: undefined local variable or method `current_user' for #<Api::V1::NewsController:0x00007f9d2516b038>>"
How can I define current_user in pundit?
You can define your own current_user in ApplicationController, or use pundit_user - as mentioned in the documentation
I am new to rails, I would like to call helper method define in application_helper.rb from rails console directly, same as we do for model methods.
I have helper method defined as
module ApplicationHelper
def user_name(user)
if user.is_admin?
user.customer.name
else
user.name
end
end
end
while calling helper method I am getting following error
2.3.1 :002 > user_name(user)
NoMethodError: undefined method `user_name' for main:Object
Yes, you can use helper method from rails console
user = User.first
helper.user_name(user)
I am trying to move some code out of my controller and into a service object. How do I access the devise sign_in helper method from the service object.
class CompleteAccountRegistration
def self.call(account_id, plan_id)
#account = Account.find(account_id)
self.create_user_account
self.create_subscription(plan_id)
sign_in(User.find(#account.owner_id))
end
I am getting the following error in my tests.
NoMethodError:
undefined method `sign_in' for CompleteAccountRegistration:Class
Including Devise::Controllers::SignInOut in your service object does the trick!
I am following the example write in Chapter 14 "Logging In" of the book.
I have my view in "127.0.0.1:3000/login" working well, but if i insert my user and password it returns this error:
NoMethodError in SessionsController#create
undefined method `authenticate' for #< User:0x9f75978>
How to solve it?
create method sessions_controller.rb is:
def create
user = User.find_by_name(params[:name])
if user and user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to admin_url
else
redirect_to login_url, alert: "Invalid user/password combination"
end
end
It's trying to call authenticate on User, this means you probably don't have an authenticate method on user. If you do, make sure it's not private.
The code you give tries to call the authenticate method on an instance of a User object.
Based on you comment re: User.authenticate(name, password) you have an authenticate method only on the User class - and it takes both name and password as parameters.
To call the User class method, you'd instead use the following:
user = User.find_by_name(params[:name])
if user && User.authenticate(params[:name], params[:password])
Alternatively, look for an instance-level method called authenticate which (as #caulfield mentioned above) would look something like:
def authenticate(password)
# does stuff
end
instead of User.authenticate(name,password)
Im trying to refresh the Geokit location to session after the user sign in. I have the following code.
# app/controllers/application_controller.rb
def after_sign_in_path_for(resource_or_scope)
session[:geo_location] = User.geocode(current_user.city)
end
But I'm getting the following error.
NoMethodError in Devise::SessionsController#create
undefined method `model_name' for Geokit::GeoLoc:Class
Seems like Geokit is not loading before the devise controller. Any idea?
The problem is after_sign_in_path_for must return a valid an url object and you are not doing it check its documentation.