我有看法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>
类似的问题在这里 在这里 ,并在这里