2012-05-03 91 views
0
我的應用程序

的一些背景。我有有1個測驗和屬於用戶測驗用戶。我想列出用戶所採取的測驗,但我得到一個錯誤。Rails視圖未定義的方法

NoMethodError中的用戶#索引

顯示/Users/Daniel/Documents/cis196/CollegeConnection/app/views/users/index.html.erb其中行#24提出:

未定義的方法`的userName '的零:NilClass 提取的源(圍繞線#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:/用戶/丹尼爾/文檔/ cis196/CollegeConnection

應用程序跟蹤|框架跟蹤|全面跟蹤 應用程序/視圖/用戶/ index.html.erb:24:在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' 應用程序/控制器/ users_controller.rb:25:在'指數」

下面是一些relavent代碼。

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 %> 

這是帶有錯誤的視圖。任何幫助將是偉大的!

回答

1

你打電話從您的視圖中的實例變量@user但你不要在你的控制器的任何地方instatiate它,因此它是零(不存在)。

在調用視圖文件的控制器,你必須instatiate變量是這樣的:

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

這應該會在視圖@user可用。有關更多信息,請參閱ActiveRecord關聯指南:http://guides.rubyonrails.org/association_basics.html

+0

我仍然得到同樣的錯誤。任何其他想法?哦,並注意,該視圖是一個用戶視圖,而不是一個測驗視圖。 –

+0

錯誤顯示問題發生在'索引'控制器操作,但粘貼的代碼是'創建'操作它看起來像QuizController的創建動作,而不是UserController ...),所以任何改變它的建議(通過在那裏分配值給@user)是不太可能的) – Pavling

+0

我已經更新了我的初始答案。 – Agis

0

我注意到您正在遍歷@user,這意味着@user是一個數組對象(可能是一組用戶)。

您必須檢查@user是否在控制器操作或視圖中正確初始化。在這種情況下,檢查是否@user在users_controller,行動「指數」設定。

另外,由於它是一組用戶,請使用@users not @user。

0

我認爲行24行:

<td><h6><%= u.quiz.userName%></h6></td> 

由於錯誤是告訴你,「無」不具有「.userName」的方法,這是試圖有一個稱爲該方法的東西它必須是零。所以在你的迭代用戶中,其中一個最有可能沒有「.quiz」。

如果您使用Ruby調試,你可以在該行指向一個斷點檢查的所有值(或只是用「把u.quiz.inspect」它轉儲到控制檯。