Rails Hartl - 第13章(表“microposts”没有列名为“micropost_0”)

时间:2017-04-27 19:23:57

标签: ruby-on-rails sqlite

我有两种类型的错误。 所有迁移都已完成。 有什么想法吗?

1

错误:

  

UsersShowTest#test_show_non-actiavated_users_shouldn't_be_displayed:   ActiveRecord :: Fixture :: FixtureError:表“microposts”没有名为“micropost_0

的列

2

  

UsersShowTest#test_show_non-actiavated_users_shouldn't_be_displayed:   NoMethodError:nil的未定义方法`each':NilClass

    require 'test_helper'

class UsersProfileTest < ActionDispatch::IntegrationTest
    include ApplicationHelper

  def setup 
    @user = users(:mike)
  end

  test "profile display" do 
    get user_path(@user)
    assert_template 'users/show'
    assert_select 'title', full_title(@user.name)
    assert_select 'h1', text: @user.name
    assert_select 'h1>img.gravatar'
    assert_match @microposts.count.to_s, response.body
    assert_select 'div.pagination'
    @user.microposts.paginate(page: 1).each do |micropost|
        assert_match micropost.content, response.body
    end
  end
end

microposts.yml看起来像:

orange: 
  content: "I hate orange"
  created_at: <%= 10.minutes.ago %>
  user: mike

cat: 
  content: "I hate dogs"
  created_at: <%= "12.minutes.ago" %>
  user: mike

dog:
  content: "I hate cats"  
  created_at: <%= "20.minutes.ago" %>
  user: mike

most_recent: 
  content: "Just a short test"
  created_at: <%= Time.zone.now %>  
  user: mike

<% 30.times do |n| %>
  micropost_<%= n %>:
    content: <%= FFaker::LoremRU.sentence %>
    created_at: <%= 40.days.ago %>
    user: mike
<% end %>  

schema.rb的一部分

ActiveRecord::Schema.define(version: 20170426135432) do

  create_table "microposts", force: :cascade do |t|
    t.text     "content"
    t.integer  "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id", "created_at"], name: "index_microposts_on_user_id_and_created_at"
    t.index ["user_id"], name: "index_microposts_on_user_id"
  end

1 个答案:

答案 0 :(得分:0)

YML文件对空格非常敏感。您缩进了micropost_<%= n %>:行。如果您按如下方式编写该部分,则应该有效:

<% 30.times do |n| %>
micropost_<%= n %>:
  content: <%= FFaker::LoremRU.sentence %>
  created_at: <%= 40.days.ago %>
  user: mike
<% end %>