0

使用以下命令由脚手架方法生成的此 CURD

rails g scaffold contact fullname:string surname:string email:string mobile:string note:text

当我去 localhost:3003/contacts/new 得到以下错误

    NoMethodError in Contacts#new

Showing /root/contactlist/app/views/contacts/_form.html.erb where line #16 raised:

undefined method `fullname' for #<Contact:0xb2fa75b4>

Extracted source (around line #16):

13: 
14:   <div class="field">
15:     <%= f.label :fullname %><br />
16:     <%= f.text_field :fullname %>
17:   </div>
18:   <div class="field">
19:     <%= f.label :surname %><br />

Trace of template inclusion: app/views/contacts/new.html.erb

Rails.root: /root/contactlist
Application Trace | Framework Trace | Full Trace

app/views/contacts/_form.html.erb:16:in `_app_views_contacts__form_html_erb___387033993__645411558'
app/views/contacts/_form.html.erb:1:in `_app_views_contacts__form_html_erb___387033993__645411558'
app/views/contacts/new.html.erb:3:in `_app_views_contacts_new_html_erb__1061805842__644918458'
app/controllers/contacts_controller.rb:29:in `new'

这是我的意见/联系人/_form.html.erb

    <%= form_for(@contact) do |f| %>
  <% if @contact.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@contact.errors.count, "error") %> prohibited this contact from being saved:</h2>

      <ul>
      <% @contact.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :fullname %><br />
    <%= f.text_field :fullname %>
  </div>
  <div class="field">
    <%= f.label :surname %><br />
    <%= f.text_field :surname %>
  </div>
  <div class="field">
    <%= f.label :email %><br />
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :mobile %><br />
    <%= f.text_field :mobile %>
  </div>
  <div class="field">
    <%= f.label :note %><br />
    <%= f.text_area :note %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

这是控制器文件“contacts_controller.rb”

 class ContactsController < ApplicationController
      # GET /contacts
      # GET /contacts.json
      def index
        @contacts = Contact.all

        respond_to do |format|
          format.html # index.html.erb
          format.json { render :json => @contacts }
        end
      end

      # GET /contacts/1
      # GET /contacts/1.json
      def show
        @contact = Contact.find(params[:id])

        respond_to do |format|
          format.html # show.html.erb
          format.json { render :json => @contact }
        end
      end

      # GET /contacts/new
      # GET /contacts/new.json
      def new
        @contact = Contact.new

        respond_to do |format|
          format.html # new.html.erb
          format.json { render :json => @contact }
        end
      end

      # GET /contacts/1/edit
      def edit
        @contact = Contact.find(params[:id])
      end

      # POST /contacts
      # POST /contacts.json
      def create
        @contact = Contact.new(params[:contact])

        respond_to do |format|
          if @contact.save
            format.html { redirect_to @contact, :notice => 'Contact was successfully created.' }
            format.json { render :json => @contact, :status => :created, :location => @contact }
          else
            format.html { render :action => "new" }
            format.json { render :json => @contact.errors, :status => :unprocessable_entity }
          end
        end
      end

      # PUT /contacts/1
      # PUT /contacts/1.json
      def update
        @contact = Contact.find(params[:id])

        respond_to do |format|
          if @contact.update_attributes(params[:contact])
            format.html { redirect_to @contact, :notice => 'Contact was successfully updated.' }
            format.json { head :no_content }
          else
            format.html { render :action => "edit" }
            format.json { render :json => @contact.errors, :status => :unprocessable_entity }
          end
        end
      end

  # DELETE /contacts/1
  # DELETE /contacts/1.json
  def destroy
    @contact = Contact.find(params[:id])
    @contact.destroy

    respond_to do |format|
      format.html { redirect_to contacts_url }
      format.json { head :no_content }
    end
  end
end

这是模型文件“contact.rb”

class Contact < ActiveRecord::Base
  attr_accessible :email, :fullname, :mobile, :note, :surname
end

请帮我解决这个错误,我被卡住了。

4

1 回答 1

1

一切看起来都不错。运行rake db:migrate然后重新启动服务器。

编辑:

如果您已经创建了表并且想要重新创建它,请先删除旧表

将此添加到迁移的顶部

def change
  drop_table contacts
  .... #create the new contact table
end
于 2012-05-29T18:26:18.307 回答