I am getting this error:
undefined method `posts' for nil:NilClass
I have a model Post, here is my PostsController
def new
#post = current_user.posts.build
end
def create
#user = current_user
#post = #user.posts.build(post_params)
end
The current_user field in your code is not getting set, and has a value of nil. Hence, when you try to access current_user.posts, it translates to fetching posts for a nil class object. As there is no such method for objects of nilclass, we get an error.
Related
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.
I am getting the error undefined method `questions' for nil:NilClass when viewing different profiles. I am not seeing why this is an undefined method. Anyone have a clue what I did not do correct?
Logs:
NoMethodError (undefined method `questions' for nil:NilClass):
app/controllers/users_controller.rb:80:in `show'
Users Controller:
def show
#user = User.find_by(username: params[:id])
The error is letting you know that the #user variable is not being set. Check your before_action/before_filter or whatever is setting that variable to make sure it is not failing.
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
I want to update attributes of a model:
def update
MyModel.update_attributes params[:id], params[:mymodel]
#.....
end
But it says undefined method `update_attributes' for #<Class:0x0000000396ecb0>. I wonder, isn't this the same as https://stackoverflow.com/a/840323/1708058
update_attributes is an instance method of ActiveRecord::Relation, you have to use the class method update:
MyModel.update(params[:id], params[:mymodel])
To use update_attributes you could do:
#my_model = MyModel.find(params[:id])
#my_model.update_attributes(params[:mymodel])
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