-1

This error is coming up after pressing submit on my form saving to db as I'm taking a course in Ruby. I have also already attempted rake:db migrate to no avail.

ActiveRecord::UnknownAttributeError in ContactsController#create
unknown attribute: comments

Extracted source (around line #7): 5 6 7 8 9 10

def create @contact = Contact.new(contact_params)

if @contact.save
  redirect_to new_contact_path, notice: "Message sent."

my contact controler.rb code

class ContactsController < ApplicationController

 def new
 @contact = Contact.new
 end

 def create
    @contact = Contact.new(contact_params)

    if @contact.save
      redirect_to new_contact_path, notice: "Message sent."
    else
      redirect_to new_contact_path, notice: "Error occurred."
    end
 end

 private
    def contact_params
      params.require(:contact).permit(:name, :email, :comments)
    end
end

My contact.rb

class Contact < ActiveRecord::Base
    def name
    end

    def email
    end

    def comments
    end

end

--------------

    class CreateContacts < ActiveRecord::Migration
 def change
    create_table :contacts do |t|
      t.string :name
      t.string :email
      t.text :commments

      t.timestamps
    end
 end
end

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Zurch
  • 39
  • 10

2 Answers2

1

In your migration file the column comments has 3m's (:commments) instead of two

def change 
     create_table :contacts do |t| 
             t.string :name 
             t.string :email 
             t.text :commments

             t.timestamps
end

Now you have to change the column by creating a migration How can I rename a database column in a Ruby on Rails migration?

Community
  • 1
  • 1
BENS
  • 185
  • 2
  • 7
  • Thanks i was able to get it working this method as the data was not important at this time "in this case, better use rake db:rollback. Then edit your migration and again type rake db:migrate. However, if you have data in the column you don't want to lose, then use rename_column." – Zurch Mar 07 '16 at 22:39
  • db:rollback is good , if you dont have any important data .glad it worked for you , best of luck with your app ;) – BENS Mar 07 '16 at 22:46
0

Is :comments a db field in your Contact model? It should be for it to work in here:

def 
    contact_params params.require(:contact).permit(:name, :email, :comments) 
end
jdabrowski
  • 1,805
  • 14
  • 21
  • thanks model appears as: class Contact < ActiveRecord::Base def name end def email end def comments end end – Zurch Mar 07 '16 at 21:51