rspec undefined 方法 'id' for nil:NilClass


       require 'rails_helper'
      feature "comment" do
        given(:current_user) do
          create(:user)
        end
        given(:undertaking) do
           create(:undertaking)
        end
        background do
         login_as(current_user)
        end
        scenario "can create comment" do
          #below two because undertaking = user_id:2 & asking_id:1
          create(:user)
          create(:asking)
          p undertaking
          p Asking.find(1)
          p User.find(2)
          p User.find(1)
          p Undertaking.all
          visit undertaking_path(undertaking)
          expect(current_path).to eq undertaking_path(1)
          within("form#undertake-form-test") do
           fill_in "content" , with: "heyheyhey"
          end
          click_button 'Send'
          expect(page).to have_content 'heyheyhey'
        end
       end

这是spec/features/comment_spec.rb。以下是结果命令rspec。

             #<Undertaking id: 1, title: "MyString", content: "MyText", result: false, user_id: 2, asking_id: 1, created_at: "2016-12-13 15:07:08", updated_at: "2016-12-13 15:07:08">
              #<Asking id: 1, content: "MyText", fromlang: "MyString", tolang: "MyString", usepoint: 1, finished: false, title: "MyString", deadline: nil, user_id: 1, created_at: "2016-12-13 15:07:08", updated_at: "2016-12-13 15:07:08">
              #<User id: 2, email: "shiba.hayato2@docomo.ne.jp", created_at: "2016-12-13 15:07:08", updated_at: "2016-12-13 15:07:08", provider: nil, uid: nil, name: "Shiruba", occupation: "大学生", age: 10, sex: "男性", content: "heyheyheyeheyeheye", skill: "日本語検定3級", picture: "/assets/default_user.jpg", point: 500, country: "Japan", language1: "Japanese", language2: "Korea", language3: "English">
             #<User id: 1, email: "shiba.hayato1@docomo.ne.jp", created_at: "2016-12-13 15:07:08", updated_at: "2016-12-13 15:07:08", provider: nil, uid: nil, name: "Shiruba", occupation: "大学生", age: 10, sex: "男性", content: "heyheyheyeheyeheye", skill: "日本語検定3級", picture: "/assets/default_user.jpg", point: 500, country: "Japan", language1: "Japanese", language2: "Korea", language3: "English">
            #<ActiveRecord::Relation [#<Undertaking id: 1, title: "MyString", content: "MyText", result: false, user_id: 2, asking_id: 1, created_at: "2016-12-13 15:07:08", updated_at: "2016-12-13 15:07:08">]>
            F
            Failures:
            1) comment can create comment
                Failure/Error: <%= @undertaking.id %>
                ActionView::Template::Error:
                      undefined method `id' for nil:NilClass

下面是unterthawing_controller.rb。

       class UndertakingController < ApplicationController
              def show
               @undertaking=Undertaking.find(params[:id])
               @comment=Comment.new do |c|
                c.user=current_user
               end
              end
       end

下面是进行/show.html.erb。

               <%= @undertaking.id %>

为什么我有错误?为什么@UnderTaking在nil中却是nil,虽然是企业。

我认为这与您的控制器使用的命名有关。该公约是企业/show.html.erb供该视图而不是进行/show.html.erb。我也会使用

 class UndertakingsController < ApplicationController

而不是

class UndertakingController < ApplicationController

最后,我会检查我所有的路线还具有正确的命名。希望有帮助。祝你好运

相关内容

最新更新