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.
Related
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.
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'm just starting out with Rails and I got stuck with displaying properties that only belongs to signed in user.
I have a user, post model with the following associations
class User < ActiveRecord::Base
has_many :posts, dependent: :destroy
end
class Post < ActiveRecord::Base
belongs_to :user
end
and on post controller I'm trying to access current user's posts with session helper method called current_user but it complains that posts is undefined
def index
#posts = current_user.posts
end
This is the helper method
def current_user
remember_token = User.digest(cookies[:remember_token])
#current_user ||= User.find_by(remember_token: remember_token)
end
Can someone please shed some light on it?
Error Message
NoMethodError in PostsController#index
undefined method `posts' for nil:NilClass
It's is not the method posts is undefined. Your current_user object is nil and that means the user with this remember_token is not found. If you never want a undefined method posts for nil:NilClass type in current_user.try(:posts) so that if the user is logged out they won't see the error. Or create this method in your application controller.
application_controller.rb
private
def is_logged_in?
current_user.present?
end
And call this method in the before_filter section of your controller.
Anyways why not use devise gem for your authentication process. It's is very reliable and comes with the current_user and authenticate_user! methods defined out of the box
It says that the value your current_user is nil, and nil does not have method posts.
Check your user's remember token, and print the parameter you feed to find_by.
Eeeer... is it complaining on the view or the controller? tell us exactly what it complains about (copy paste the result). Maybe use #posts in your view instead of posts (ofcourse posts is undefined, #posts is defined).
My new controller action:
controller do
layout 'active_admin'
def index
#pages = Page.all
end
end
After refresh the page i received:
undefined method `base' for nil:NilClass
render view_factory.layout
What should I do for fixing this?
I start rewriting controller action because i received this message for my index action:
undefined method `num_pages' for #<Array:0x0000000b860eb0>
render renderer_for(:index)
Maybe anyone know how fixing this?
The initial undefined method 'num_pages' for #<Array:0x0000000b860eb0> may be occurring if you have an instance variable set in a before_filter in ApplicationController with the plural name of a model as I did. The bug is reported here.
Would need to see the code on the view page for this but it sounds to me like you are making a call for num_pages on an object that is an array class. Since Ruby's array class has no num_pages method, it is throwing an error.
Jamie you are right! But then i received this message for my index action:
undefined local variable or method `per' for ActiveRecord::Relation
And I fix this problem by doing this:
# config/initializers/will_paginate.rb
if defined?(WillPaginate)
module WillPaginate
module ActiveRecord
module RelationMethods
alias_method :per, :per_page
alias_method :num_pages, :total_pages
end
end
end
end
The bug is reported here.
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