【发布时间】:2016-03-07 21:09:46
【问题描述】:
当我正在学习 Ruby 课程时,在保存到 db 的表单上按提交后出现此错误。我也已经尝试过rake:db migrate 无济于事。
ActiveRecord::UnknownAttributeError in ContactsController#create
unknown attribute: comments
提取的源代码(第 7 行附近): 5 6 7 8 9 10
定义创建 @contact = Contact.new(contact_params)
if @contact.save
redirect_to new_contact_path, notice: "Message sent."
我的联系人controler.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
【问题讨论】:
标签: html ruby-on-rails-4 attributes