ContactsController中的UnknownAttributeError #create

时间:2016-03-07 21:09:47

标签: html ruby-on-rails-4 attributes

在我将表格保存到数据库后按下提交后出现此错误,因为我正在学习Ruby课程。我也已经尝试rake:db迁移无济于事。

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

提取的来源(第7行): 五 6 7 8 9 10

def创建     @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

我的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

2 个答案:

答案 0 :(得分:1)

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

select t1.popular, t2.recent, t3.matches
from       (SELECT @rownum1 := @rownum1 + 1 AS rank, `text` AS `popular` FROM `searches` t, (SELECT @rownum1 := 0) r WHERE `text` LIKE 'Tyr%' ORDER BY `hits`     DESC LIMIT 10) t1
inner join (SELECT @rownum2 := @rownum2 + 1 AS rank, `text` AS `recent`  FROM `searches` t, (SELECT @rownum2 := 0) r WHERE `text` LIKE 'Tyr%' ORDER BY `datetime` DESC LIMIT 10) t2 on t1.rank = t2.rank
inner join (SELECT @rownum3 := @rownum3 + 1 AS rank, `text` AS `matches` FROM `searches` t, (SELECT @rownum3 := 0) r WHERE `text` LIKE 'Tyr%' ORDER BY `matches`  DESC LIMIT 10) t3 on t2.rank = t3.rank

现在您必须通过创建迁移来更改列 How can I rename a database column in a Ruby on Rails migration?

答案 1 :(得分:0)

:comments模型中的Contact是否为db字段?它应该在这里工作:

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