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 7899587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:44:30+00:00 2026-06-03T08:44:30+00:00

A little background for my app. I have users that have 1 quiz and

  • 0

A little background for my app. I have users that have 1 quiz and a quiz that belongs to a user. I am trying to list the quizzes that the users have taken but I get an error.

NoMethodError in Users#index

Showing /Users/Daniel/Documents/cis196/CollegeConnection/app/views/users/index.html.erb where line #24 raised:

undefined method `userName’ for nil:NilClass
Extracted source (around line #24):

<% @user.each do |u| %>
  <tr>
    <td><h6><%= u.quiz.userName%></h6></td>
    <td><h6><%= u.quiz.q1 %></h6></td>
    <td><% for q in u.quiz.q2 %>
      <% if q != nil %>

Rails.root: /Users/Daniel/Documents/cis196/CollegeConnection

Application Trace | Framework Trace | Full Trace
app/views/users/index.html.erb:24:in block in _app_views_users_index_html_erb__3921348574137420689_70261694365660'
app/views/users/index.html.erb:22:in
_app_views_users_index_html_erb__3921348574137420689_70261694365660′
app/controllers/users_controller.rb:25:in `index’

Here’s some relavent code.

class User < ActiveRecord::Base

has_one :quiz, :class_name => 'Quiz'

attr_accessible :name, :email, :password, :password_confirmation, :position, :remember_me
validates :name, :presence => true
validates_length_of :name, :maximum=>30
validates :name, :format => { :with => /\A[a-zA-Z]+\z/,
:message => "Only letters allowed" }
validates_presence_of :position
validates :email, :presence => true
validates :email, :uniqueness => true

scope :college,    where(position: 'College')
scope :highschool, where(position: 'High School')

end


class Quiz < ActiveRecord::Base
belongs_to :user

validates :q1, :presence => true
validates :q2, :presence => true
validates :q3, :presence => true
serialize :q2
has_many :activities



 end

def create
@quiz = current_user.quizzes.build(params[:quiz])
# @quiz = Quiz.new(params[:quiz])
@quiz.userName=current_user.name
@activities = Activity.all

respond_to do |format|
  if @quiz.save
    format.html { redirect_to @quiz, notice: 'Thank You for Taking the Quiz!.' }
    format.json { render json: @quiz, status: :created, location: @quiz }
  else
    format.html { render action: "new" }
    format.json { render json: @quiz.errors, status: :unprocessable_entity }
  end
end
end

<% @user.each do |u| %>
<tr>
  <td><h6><%= u.quiz.userName%></h6></td>
  <td><h6><%= u.quiz.q1 %></h6></td>
  <td><% for q in u.quiz.q2 %>
  <% if q != nil %>
    <h6><%= q  %></h6>
  <% end %>
<% end %>
<td><h6><%= u.quiz.q3 %></h6></td>
<td>X</td>
</tr>
<% end %>

This is the view with the error. Any help would be great!

  • 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-06-03T08:44:31+00:00Added an answer on June 3, 2026 at 8:44 am

    You’re calling the instance variable @user from your view but you don’t instatiate it anywhere in your controller, therefore it’s nil (doesn’t exist).

    In the controller that calls the view file, you have to instatiate the variable like this:

    # assuming you've already did something like
    # @quiz = Quiz.find(params[:id]
    # in order to retrieve the quiz
    @user = @quiz.user
    

    This should make @user available in the view. For more info refer to the ActiveRecord Associations guide: http://guides.rubyonrails.org/association_basics.html

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

Sidebar

Related Questions

A little background info; I'm making an iOS App that uses facebook integration. I
First a little background: I have an Event model that has various event_type s.
I have a little iPhone app that loads data from a web service. To
First, a little background: We have an intranet site based on WSS 3.0 that
I have a little idea for an App, but in order to start I
Little Background: I have csv file which has lots of rows and each row
A little background: I have a perl script which is performing a number of
Ok guys so a little background... I'm writing a small application that will assign
First a little background: I have already managed to connect to a Microsoft SOAP
A little background: My app is designed as a web form in a UITableView.

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.