#<Model:0x000...> 的未定义方法“NameOfField" [英] Undefined Method &#39;NameOfField&#39; for #&lt;Model:0x000...&gt;

查看:43
本文介绍了#<Model:0x000...> 的未定义方法“NameOfField"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全被这个错误难住了.非常感谢一些帮助:)

I'm totally stumped on this error. Would really appreciate some help :).

要重现错误,您可以从 https://github.com/WaleyChen/twitter_clone.然后运行bundle exec rspec spec/".

To reproduce the error, you can pull the program from https://github.com/WaleyChen/twitter_clone. Then run 'bundle exec rspec spec/'.

我对控制器进行了 rspec 测试,定义为:

I have an rspec test for my controller defined as:

require 'spec_helper'

describe FrontpageController do
  render_views # render the views inside the controller tests, so not just test the actions

  describe "GET 'frontpage'" do
    it "should be successful" do
      get 'frontpage'
      response.should be_success
    end

    it "should have the right title" do
      get 'frontpage'
      response.should have_selector("title", :content => "Twitter")
    end
  end

end

运行 rspec 测试时,出现以下错误:

When I run my rspec tests, I get the following error:

Failures:

1) FrontpageController GET 'frontpage' should be successful
 Failure/Error: get 'frontpage'
 ActionView::Template::Error:
   undefined method `full_name' for #<User:0x007fbfce43dce0>
 # ./app/views/frontpage/frontpage.html.erb:22:in `block in _app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
 # ./app/views/frontpage/frontpage.html.erb:21:in `_app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
 # ./spec/controllers/frontpage_controller_spec.rb:8:in `block (3 levels) in <top (required)>'

2) FrontpageController GET 'frontpage' should have the right title
 Failure/Error: get 'frontpage'
 ActionView::Template::Error:
   undefined method `full_name' for #<User:0x007fbfcc99a410>
 # ./app/views/frontpage/frontpage.html.erb:22:in `block in _app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
 # ./app/views/frontpage/frontpage.html.erb:21:in `_app_views_frontpage_frontpage_html_erb___4518234645475110659_70230885952360'
 # ./spec/controllers/frontpage_controller_spec.rb:13:in `block (3 levels) in <top (required)>'

这是控制器:

class FrontpageController < ApplicationController
  def frontpage
    @user = User.new
    @sign_up = User.new
  end

  def sign_up
    if @post.save
      format.html { redirect_to @post, notice: 'Post was successfully created.' }
    end
  end
end

这是导致错误的视图:

<%= form_for @user, :url =>"sign_up" do |form| %>
  <%= form.text_field :full_name, :placeholder => "Full name" %>
  </br>
  <%= form.text_field :email, :placeholder => "Email" %>
  </br>
  <%= form.text_field :pw, :placeholder => "Password" %>
  </br>
  <%= form.text_field :username, :placeholder => "Username" %>
  </br>
  <%= form.submit "Sign up" %>
<% end %>

这是 user.rb:

Here's user.rb:

class User < ActiveRecord::Base
end

这里是 schema.rb:

Here's schema.rb:

ActiveRecord::Schema.define(:version => 20111106084309) do

  create_table "users", :force => true do |t|
    t.string   "full_name"
    t.string   "email"
    t.string   "username"
    t.string   "pw"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

推荐答案

您需要确保您的测试数据库在所有迁移中都是最新的:

You need to make sure your test database is up to date with all the migrations:

rake db:test:prepare

但是,您可能会遇到问题,因为您的迁移已损坏;您有两个名为CreateUsers"的迁移,Rails 会抱怨这些迁移.看起来您应该删除最近的那行,然后取消注释原始行中的 t.string :email 行.

However, you may have a problem with that as your migrations are broken; you have two migrations named "CreateUsers" which Rails will complain about. It looks like you should delete the more recent one, and then uncomment the t.string :email line in the original one.

此外,如果您使用 bundle exec rake rspec 而不是 bundle exec rspec spec,它将确保您的数据库迁移在运行测试之前是最新的.我刚刚克隆了您的 repo 并执行了此操作,并且它通过了测试.

Also, if you use bundle exec rake rspec instead of bundle exec rspec spec it'll make sure your database migrations are up to date before running the tests. I just cloned your repo and did this and it passed the tests just fine.

这篇关于#<Model:0x000...> 的未定义方法“NameOfField"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆