0

次のコマンドを使用してscaffoldメソッドによって生成されたこの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'

これは私のviews/contacts /_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 に答える