未知属性联系人控制器中的错误#创建


当我

在 Ruby 上课时,在我的表单保存到 db 上按提交后会出现此错误。我也已经尝试rake:db迁移无济于事。

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

提取的源(围绕第 #7 行):5678910

定义创建 @contact = 联系人.新(contact_params)

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

我的联系人控制器.rb 代码

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

我的联系人.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

在您的迁移文件中,列注释有 3m 的 (:commments) 而不是两个

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

现在,您必须通过创建迁移来更改列如何在 Ruby on Rails 迁移中重命名数据库列?

:comments Contact模型中的数据库字段吗?它应该在这里工作:

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

最新更新