我正在查看 show.html.erb
<%= @question.user.lesson_id %>
这是浏览器上的一个数字。
我的 questions_controller.rb 是
def show
@question = Question.find(params[:id])
@comments = @question.comments.all
if user_signed_in?
@rating_currentuser = @question.ratings.find_by_user_id(current_user.id)
unless @rating_currentuser
@rating_currentuser = current_user.ratings.new
end
end
和我的 questions_controller_test.rb
test "should show question signed in" do
sign_in users(:user2)
get :show, :id => @question_user1.to_param
assert_response :success
end
一切正常(浏览器和测试)
当改变视图 show.html.erb
<%= @question.user.lesson.name %>
我对浏览器没问题,但测试失败
ActionView::Template::Error: undefined method `name' for nil:NilClass.
我试试这个
@name = Lesson.find(params[:id])
在 questions_controller.rb 然后测试失败
Expected response to be a <:success>, but was <302>