undefined method for Questions - ruby-on-rails

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.

Related

Pundit defining current_user

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

undefined method `posts' for nil:NilClass error

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.

undefined method `model_name' for TrueClass:Class

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.

Error Undefined method `admin?' for nil:NilClass - Seems simple won't work

I have a small problem that ive tried to fix for about an hour. It seems so simple.
If someone goes to the user index view i want them redirected to root url. Unless they're admin.
Any clues how to fix?
Thank you for any help in advance.
I have a simple test
test "should redirect index when not logged in" do
get :index
assert_redirected_to login_url
end
But i receive this error.
1) Error:
UsersControllerTest#test_should_redirect_index_when_not_logged_in:
NoMethodError: undefined method `admin?' for nil:NilClass
app/controllers/users_controller.rb:78:in `admin_user'
test/controllers/users_controller_test.rb:42:in `block in <class:UsersControllerTest>'
My admin_user method in User Controller is below
# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
end
You need to check if your user has been logged in. If he is not, current_user will be nil. You may want to check this in admin_user method too.
redirect_to(root_url) unless current_user.try(:admin?)
current_user is nil.
So something like this:
redirect_to(root_url) unless current_user && current_user.admin?
Use the rails try method to add all the objects.
redirect_to(root_url) unless current_user.try(:admin?)

NoMethodError: undefined method `deliver_now'

I have this in my controller:
def create
#user = User.new(user_params)
if #user.save
UserMailer.account_activation(#user).deliver_now
....
end
And I keep getting this error:
NoMethodError: undefined method `deliver_now' for #<ActionMailer::MessageDelivery:0x007ffa6646eb60>
Any ideas why it's not finding the deliver_now method?
deliver_now has been introduced in rails 4.2.beta2. You are probably using earlier version.
I think it's because it's deliver (or deliver! for the bang version) and not deliver_now.
See ActionMailer::Base
deliver works instead of deliver_now for older versions of Rails.

Resources