Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7693011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:59:07+00:00 2026-05-31T20:59:07+00:00

I used this code in my users views and had no trouble: <% if

  • 0

I used this code in my “users” views and had no trouble: <% if current_user.admin? %>. But using it in a set of views associated with a different controller throws up the “No method Error.”

Background: the app allows admins to create scavenger hunts. Admins should be able to delete hunts. I thought I knew how to configure everything, but apparently, I’m missing something. Here’s my code:

controller.rb

class HuntsController < ApplicationController

  def index
     @title = "All Hunts"
     @hunts = Hunt.order("name ASC") 
  end

  def show
    @hunt = Hunt.find(params[:id])
    @title = @hunt.name    
  end

  def new
    @hunt = Hunt.new
    @title = "New Hunt"  
  end

  def create
    @hunt = Hunt.new(params[:hunt])
    if @hunt.save
      flash[:success] = "Hunt created!"
      redirect_to hunts
    else
      @title = "New Hunt"
      render 'new'     
    end
  end

  def edit
    @hunt = Hunt.find(params[:id])
    @title = "Edit hunt"
  end

  def delete
    Hunt.find(params[:id]).destroy
    flash[:success] = "Hunt destroyed."
    redirect_to index  
  end

end

Views/Index.html.erb

    <h1>All Hunts</h1>

    <ul>
      <% @hunts.each do |hunt| %>
         <%= render hunt  %>
      <% end %>
    </ul>

    <%= link_to( "Create New Hunt", '/hunts/new') %> 

Views/_hunt.html.erb

<li>
  <%= link_to hunt.name, hunt %>
  <% if current_user.admin? %>
   <%= link_to "delete", hunt, :method => :delete, :confirm => "You sure?",
                                :title => "Delete #{hunt.name}" %>
  <% end %>
</li>

Error Message when trying to head to /hunts:

NoMethodError in Hunts#index
Showing ...../app/views/hunts/_hunt.html.erb where line #3 raised:
undefined method `admin?' for nil:NilClass
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:59:08+00:00Added an answer on May 31, 2026 at 8:59 pm

    current_user is nil, and thus does not know how to respond to admin?. Either ensure that current_user is always a user instance, or check that it’s not nil.

    In Ruby 2.3+, one can use the “safe navigation” operator (&.):

    if current_user&.admin?
    

    In Ruby 2.2 and earlier, instead use boolean short-circuiting:

    if current_user && current_user.admin?
    

    Note that ActiveSupport has try, but that has different behavior which will potentially hide bugs. For similar behavior, use try! instead.

    Getting “undefined method _____ for nil:NilClass” is a very common occurrence in Ruby, so get used to it happening often :).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used this code previously in netbeans 6.9.1 but it does not seem to
I have used this code for extracting urls from web page.But in the line
I successfully used this code within a powerpoint odule, but when I move it
Am trying to hide this publish 'tick-box' from non-admin users. I used the CanCan
On a Vista dev machine I used this code successfully to change user Administrator
I used this code to print the current page: <form> <input type=button value=Print onClick=window.print();
I used this code in my Android application: import org.apache.http.client.methods.HttpPost; ... HttpClient httpclient =
I used this code for sending and returning result. <script type=text/javascript> $(document).ready(function() { $('.special').click(function(){
I used this code to access the Specific XML under the SharePoint Mapped Folder;
I used this code: const char *propName = property_getName(property); NSString *propertyName = [NSString stringWithCString:propName];

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.