我的发言背景不多。 我有用户,有1台qui和1台qui,属于用户。 我正试图列举用户已经采取但我有错误的说法。
用户数指数
显示/Users/Daniel/Documents/cis196/CollegeConnection/app/views/users/index.html。 第24号线的位置:
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 %>
铁路: /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
这里有一些改写法。
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 %>
这是对错误的看法。 任何帮助都是巨大的。