Undefined Method 'NameOfField' for #<Model:0x000…>

若如初见. 提交于 2019-12-11 13:42:01

问题


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

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

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

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)>'

Here's the controller:

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

Here's the view, that's causing the error:

<%= 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 %>

Here's user.rb:

class User < ActiveRecord::Base
end

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

回答1:


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

rake db:test:prepare

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.

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.



来源:https://stackoverflow.com/questions/8026074/undefined-method-nameoffield-for-model0x000

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!