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!
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
Im getting this error after updating a has_one field
undefined method `model_name' for TrueClass:Class
This is the code, on after_sign_up of Devise
class RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
#dash = Dashboard.create(name: "David")
#user = current_user
#user.dashboard_id = #dash.id
#user.save
end
end
The code works, but give the error in the end.
The error message is a bit misleading. after_sign_up_path_for expects you to return a path. The last statement in your method (#user.save) returns a boolean. The error should disappear if you add a (return) statement at the end of your method to provide the path.
In a create method in a controller I have:
if logged_in_admin?
#invitation.set_ids
In the Invitation model:
def set_ids
self.person_one_id = current_user.id
end
current_user is a method in app/helpers/sessions_helper.rb and defines the currently logged in user. I use this method successfully in many controller methods. However, for the use case above I get the error message undefined local variable or method 'current_user' for #<Invitation:0x007f699086bf40>.
Why do I get this error message? Is this because this time I'm using the helper method in a model file and is this not allowed? If such is not allowed, what would be the best way to securely set person_one_id for #invitation equal to the id of the currently logged in user?
current_user not available in a model layer(it's MVC, your helpers on the CV layer and model know nothing about the current_user helper). Pass user_id from your helper as argument:
some_helper.rb
def my_helper
if logged_in_admin?
#invitation.set_ids(current_user.id)
# .....
model.rb:
def set_ids(user_id)
self.person_one_id = user_id
end
You have to add the following line to your ApplicationController:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper
end
Now you should be able to use the methods inside your controllers / models.
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)
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])