0

This CURD generated by scaffold method using following command

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

when i go to localhost:3003/contacts/new getting following error

    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'

This is my 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 %>

this is the Controller file "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

This is the Model file "contact.rb"

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

Please help me solve this error i'm stuck.

Gihan Dilusha
  • 891
  • 4
  • 14
  • 22
  • when run rake db:migrate this comes == CreateContacts: migrating ================================================= -- create_table(:contacts) rake aborted! An error has occurred, this and all later migrations canceled: SQLite3::SQLException: table "contacts" already exists: CREATE TABLE "contacts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "fullname" varchar(255), "surname" varchar(255), "email" varchar(255), "mobile" varchar(255), "note" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /root/contactlist/db/migrate/20120529175252_create_contacts.rb:3:in `change' – Gihan Dilusha May 29 '12 at 18:27
  • @Dave Newton which means alredy table updated isn't it? – Gihan Dilusha May 29 '12 at 18:28
  • 1
    Depends; I can't see the table from here. It means the table exists; it doesn't mean there's a `fullname`, or that you're running against the right DB, etc. – Dave Newton May 29 '12 at 18:31

1 Answers1

1

Everything looks right. Run rake db:migrate then restart the server.

Edit:

if you have created the table already and you want to recreate it then drop the old table first

Add this to the top of the migration

def change
  drop_table contacts
  .... #create the new contact table
end
Sully
  • 14,672
  • 5
  • 54
  • 79
  • when run rake db:migrate this comes == CreateContacts: migrating ================================================= -- create_table(:contacts) rake aborted! An error has occurred, this and all later migrations canceled: SQLite3::SQLException: table "contacts" already exists: CREATE TABLE "contacts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "fullname" varchar(255), "surname" varchar(255), "email" varchar(255), "mobile" varchar(255), "note" text, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /root/contactlist/db/migrate/20120529175252_create_contacts.rb:3:in `change' – Gihan Dilusha May 29 '12 at 18:31
  • there was a typo, how does your migration looks like? – Sully May 29 '12 at 18:40