Ruby-on-Rails와 mongoDB에서 간단한 연락처 북 API를 사용하고 연락처를 사용자에게 할당하려고 할 때 직면합니다.NoMethodError in Controller # create
연락 모델 :
class Contact
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Attributes::Dynamic
field :name, type: String
field :address, type: String
field :surname, type: String
field :email, type: String
field :phone, type: String
field :birthday, type: Date
field :notes, type: String
belongs_to :user
end
사용자 모델 (유증에 의해 생성) :
class User
include Mongoid::Document
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
field :email, type: String, default: ""
field :encrypted_password, type: String, default: ""
...
has_many :contacts, dependent: :destroy
end
그리고 ContactsController에서 방법을 만들 :
def create
**@contact = @user.contact.new(contact_params)**
respond_to do |format|
if @contact.save
format.html { redirect_to @contact, notice: 'Contact was successfully created.' }
format.json { render :show, status: :created, location: @contact }
else
format.html { render :new }
format.json { render json: @contact.errors, status: :unprocessable_entity }
end
end
end
그래서, 목표는 접촉을 할당하는 것입니다 현재 사용자에게 표시하고 현재 사용자에게 자신의 대화 상대 만 표시하도록 허용합니다. 어떤 제안?
감사합니다 :) 당신이 contacts
(복수)가 User
에서