1

我有一个经过身份验证的登录和一个联系人模块......这个想法是,用户有多个联系人......联系人类有名称和号码作为它的属性......但是当我尝试创建一个联系人时,它会抛出一个错误说明"unknown attribute: user_id"......我哪里出错了?我尝试添加user_id联系人模型...但仍然收到错误...帮助将不胜感激..

联系方式:

class Contact < ActiveRecord::Base
  belongs_to :user
  attr_accessible :name, :number, :user_id
end

联系人控制器:

class ContactsController < ApplicationController

  def new

  end

  def show
    @contacts=current_user.contacts
    @contacts.save
  end

  def index
    @contact=current_user.email_id
  end

  def create
 #  @contact=contacts.new
    @contact= current_user.contacts.build( :name=> params[:name] , :number=>params[:number] )
    @contact.save
    redirect_to contacts_show_path
  end

end
4

1 回答 1

1

您需要添加has_many :contacts到您的用户模型以及将“user_id”列添加到您的联系人迁移文件中。

class User < ActiveRecord::Base
  has_many :contacts
end
于 2013-02-25T05:21:26.817 回答