I have a controller, in this controller #current user is OK in show, new, create, but doesn't work in def _params, for example, #current_user.role:
undefined method `role' for nil:NilClass.
Thanks.
def company_params
if #current_user.role.name != 'admin'
params[:company_id] = #current_user.company.id
end
params.require(:company).permit(.........)
end
ERROR: undefined method `role' for nil:NilClass
#current_user is defined in a filter at the begginng but just for the defined actions. The company params doesn't seem to be included in the list. Try adding it.
before_action :set_current_user, only: [:balbalba, :company_params] #might look different
Related
I can't make head or tail of this (but I'm probably being an idiot). I have a class ApplicationController that contains the following method:
def logged_in?
!current_user.nil?
end
if logged_in?
load_and_authorize_resource :unless => :devise_controller?, :except => :show
end
This gives me an error:
undefined method `logged_in?' for ApplicationController:Class
This method is clearly defined, how is it coming back as undefined?
Maybe the best place to declare it is in /helpers/application_helper.rb instead of application_controller.
Also, to verify if a user is signed in, Devise provides the following helper user_signed_in?.
Source: https://github.com/plataformatec/devise
I think you forgot to write:
helper_method :logged_in?
in ApplicationController.
I cannot restrict the Project model view to a owning user. Cannot figure this one out:
Error:
undefined method `user' for #<Project::ActiveRecord_Relation:0x007f94b25dd010>
project_policy.rb
class ProjectPolicy < ApplicationPolicy
def show?
user.present? && user == record.user
end
end
Projects controller
class ProjectsController < ApplicationController
def show
#project = Project.find(params[:id])
#pages = #project.pages
authorize #projects
end
If I remove the user == record.user all works fine
application_policy file is default
Project belongs to User
User has many Projects
project.user in the console works fine.
undefined method `user' for Project::ActiveRecord_Relation:0x007f94b25dd010
Firstly, I'd assume that #projects is defined somewhere in your code using a before_filter and also I assume its returning a collection of records. If so then here is the issue
user.present? && user == record.user
Here the record in record.user will be a collection of records not a single record. So record.user just fails with that error.
Changing authorize #projects to authorize #project should solve your problem
In My ApplicationController class, I need to override the devise's current_user to use my own definition, but in some cases I also want the devise's current_user. I tried something like
alias_method :devise_current_user, :current_user
def current_user
But it doesn't seem to work. I get an undefined method current_user on the alias_method line. I have already asked this question elsewhere ( Override current_user in ApplicationController throws undefined method error ) but since it is not answered, I was wondering if I can do something like:
def devise_current_user
return Devise::Controllers::Helpers.current_user
end
What is the right way to do this?
You should be able to just do what you did:
def devise_current_user
current_user
end
I have setup a helper method within the application controller ie.
class ApplicationController < ActionController::Base
# Helpers
helper_method :current_user
# Private Methods
private
def current_user
#current_user ||= Tester.find(session[:user_id]) if session[:user_id]
end
end
If I try to access to current_user variable within a view im getting an error
#code
Welcome <%=#current_user.first_name%>
#error
undefined method `first_name' for nil:NilClass
I know the session is good. Is this the correct was to access to current_user ?
Thanks for the help
It's a method, you're trying to access an instance variable, do it like this:
Welcome <%= current_user.first_name%>
In my create method in the Videos controller, this line works:
#video = Video.new(params[:video])
However, I want to assign the video to the user, so I use this line which gives me an error:
#video = current_user.videos.new(params[:video])
This is the error:
NoMethodError in VideosController#create
undefined method `videos' for nil:NilClass
I have this in my User model:
has_many :videos
And this in my Video model:
belongs_to :user
This is in my application_controller file:
def current_user
return unless session[:user_id]
#current_user ||= User.find_by_id(session[:user_id])
end
# Make current_user available in templates as a helper
helper_method :current_user
How do I fix this error? Let me know if I should post anymore code.
Is this error only happening when the user is not logged or there's no session[:user_id]?
When not logged in:
#video = current_user.videos.new(params[:video])
...would be...
#video = nil.videos.new(params[:video])
which would give you that exact NoMethodError