Search code examples
ruby-on-railsbelongs-to

Rails View Undefined Method


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!


Solution

  • 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