在我的应用程序中,我有客户,我有联系人。客户可以有很多联系人和联系人可以有很多客户。它们通过CustomerContact表关联。问题是数据库只能根据他们的电子邮件地址拥有一个唯一的联系人。
那是什么,我想创建一个表单,用户可以在其中输入客户信息和任意数量的联系人。我用cocoon gem完成了这个。现在的问题是,没有“选择现有的联系人”只有文本字段,并且想法是当提交表单时,系统可以通过现有的电子邮件地址查看系统中是否已存在联系人而不是仅将客户分配给联系人,但也更新现有的联系信息。如果是新联系人,则只需将新联系人插入数据库并将联系人分配给客户。
现在显然这完全超出了正常的做事方式。您通常会进行某种查找,然后选择现有的联系人,但这不是老板想要的。
当我输入数据库中存在的联系人的电子邮件地址时,Rails总是抛出错误“联系电子邮件已被占用”。 “没问题我想”,对于现有的联系人,我会查看数据库并通过操纵控制器中的参数在联系人上创建一个id属性。所以我写了下面的代码:
# convert the params to a Hash so we can manuipulate them
myparams = customer_params.to_h
# if we have contacts we want to assign to the customer
if myparams['contacts_attributes']
# loop through each contact they entered
myparams['contacts_attributes'].each do |k,v|
# see if the contact exists in the database and isn't being destroyed
if Contact.exists?(email: v['email']) && v['_destroy'] != '1'
# grab the contact information
contact = Contact.where(email: v['email']).first
# just a double check that we got the contact from the database
if contact
# create an id attribute for the contact
myparams['contacts_attributes'][k]['id'] = contact.id
end
end
end
end
“美丽!!!”或者我想。当我试图保存联系时,我遇到了以下错误:
Couldn't find Contact with ID=117 for Customer with ID=
显然正在发生的事情是,当我将参数传递给Customer#new
方法时,Rails正在对CustomerContact表执行查找以尝试访问Contacts表,以便它可以获取联系人的信息。但是,由于这是一个新客户,因此尚未设置该关联,因为尚未创建客户。
如果我从contact_attributes
删除现有联系人并直接创建customer_contacts
协会怎么办!!!那么myparams['customer_contacts_attributes'] = []
发挥作用:
# convert the params to a Hash so we can manuipulate them
myparams = customer_params.to_h
# so we can create a record on the customer_contacts association
# directly if the contact already exits
myparams['customer_contacts_attributes'] = []
# if we have contacts we want to assign to the customer
if myparams['contacts_attributes']
# loop through each contact they entered
myparams['contacts_attributes'].each do |k,v|
# see if the contact exists in the database and isn't being destroyed
if Contact.exists?(email: v['email']) && v['_destroy'] != '1'
# grab the contact information
contact = Contact.where(email: v['email']).first
# just a double check that we got the contact from the database
if contact
# removed the contact
myparams['contacts_attributes'].delete(k)
# create the `customer_contact` association directly
myparams['customer_contacts_attributes'].push({'user_id': contact.id})
end
end
end
end
并且...它工作!!!!好吧有些什么。客户得到保存,数据库中已存在的联系人被分配,新联系人被创建和分配......那么问题是什么呢???好吧......如果验证因任何原因失败并且页面被重新绘制,则用户输入的现有联系人将从表单中删除。
所以我有一些工作但我真的需要帮助。我需要它,以便如果验证失败,联系人会在表单中显示备份。此外,显然这不是最好的方法。我希望有人在使用rails之前做过类似的事情并且有更好的方法来完成它。
我的关联设置如下:
class CustomerContact < ApplicationRecord
belongs_to :customer
belongs_to :contact
end
class Customer < ApplicationRecord
has_many :customer_contacts
has_many :contacts, through: :customer_contacts
accepts_nested_attributes_for :customer_contacts
accepts_nested_attributes_for :contacts, allow_destroy: true
end
class Contact < ApplicationRecord
has_many :customer_contacts
has_many :customers, through: :customer_contacts
end
联系部分设置如下,请注意我在新操作和编辑操作中都使用它:
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Phone</th>
<th>Department</th>
<th>Manager</th>
<th>
<%= link_to_add_association f, :contacts, class: 'btn btn-primary', partial: 'contact_fields', data: {
association_insertion_node: '.contact_fields', association_insertion_method: :append
} do %>
<i class="fas fa-plus"></i>
<% end %>
</th>
</tr>
</thead>
<tbody class="contact_fields">
<%= f.fields_for :contacts do |contact| %>
<%= render 'projects/contact_fields', f: contact %>
<% end %>
</tbody>
</table>
contact_fields
部分如下:
<tr class="nested-fields">
<td>
<%= f.text_field :fullname, class: 'form-control invoke-contacts-search contact-fullname' %>
</td>
<td>
<%= f.text_field :email, class: 'form-control invoke-contacts-search contact-email' %>
</td>
<td>
<%= f.text_field :phone, class: 'form-control contact-phone' %>
</td>
<td>
<%= f.text_field :department, class: 'form-control contact-department' %>
</td>
<td>
<%= f.text_field :manager, class: 'form-control contact-manager' %>
</td>
<td>
<%= link_to_remove_association f, class: 'btn btn-danger' do %>
<i class="fas fa-trash-alt"></i>
<% end %>
</td>
</tr>
这是控制器new和create action
def new
@customer = Customer.new
4.times { @customer.contacts.build }
end
def create
# convert the params to a Hash so we can manuipulate them
myparams = customer_params.to_h
# so we can create a record on the customer_contacts association
# directly if the contact already exits
myparams['customer_contacts_attributes'] = []
# if we have contacts we want to assign to the customer
if myparams['contacts_attributes']
# loop through each contact they entered
myparams['contacts_attributes'].each do |k,v|
# see if the contact exists in the database and isn't being destroyed
if Contact.exists?(email: v['email']) && v['_destroy'] != '1'
# grab the contact information
contact = Contact.where(email: v['email']).first
# just a double check that we got the contact from the database
if contact
# removed the contact
myparams['contacts_attributes'].delete(k)
# create the `customer_contact` association directly
myparams['customer_contacts_attributes'].push({'user_id': contact.id})
end
end
end
end
@customer = Customers.new(myparams)
respond_to do |format|
if @customer.save
format.html { redirect_to edit_customer_path(@customer), success: 'Customer was successfully created.' }
else
format.html { render :new }
end
end
end
以下是控制器编辑和更新操作
def edit
@customer = Customer.find(params[:id])
end
def update
myparams = customer_params.to_h
if myparams['contacts_attributes']
myparams['contacts_attributes'].each do |k,v|
if Contacts.exists?(email: v['email']) && v['_destroy'] != '1'
contact = Contact.where(email: v['email']).first
if contact
myparams['contacts_attributes'][k]['id'] = contact.id
CustomerContact.find_or_create_by(project_id: @customer.id, user_id: contact.id)
end
end
end
end
@customer.assign_attributes(myparams)
respond_to do |format|
if @customer.save
format.html { redirect_to edit_customer_path(@customer), success: 'Customer was successfully updated.' }
else
format.html { render :edit }
end
end
end
所以我终于让它工作了,但我基本上不得不重新插入嵌套属性为你做的一切,以使这个工作。它不漂亮,但效果很好。
另外......看看this post,看看我是如何在模板中进行联系人收集的
def update
myparams = customer_params.to_h
contacts_valid = true
@customer.assign_attributes(myparams)
customer_valid = @customer.valid?
@contacts = []
contacts_to_delete = []
cparams = contact_params.to_h
cparams['contacts'].each do |k, v|
if v['id'].to_i > 0
if v['_destroy'].to_i == 1
contacts_to_delete << v['id'].to_i
else
contact = Contact.find(v['id'])
contact.assign_attributes(v.except('_destroy', 'id'))
end
else
if (!v['name'].blank? || !v['email'].blank?) && v['_destroy'].to_i != 1
unless v['email'].blank?
contact = Contact.where(email: v['email']).first
end
if contact
contact.assign_attributes(v.except('_destroy', 'id'))
else
contact = Contact.new(v.except('_destroy', 'id'))
end
end
end
if contact
unless contact.valid?
contacts_valid = false
# This adds the errors from the contact to the customer object
contact.errors.full_messages.each do |m|
@customer.errors.add(:base, m)
end
end
@contacts << contact
end
end
respond_to do |format|
if customer_valid && contacts_valid
@customer.save!
@contacts.each do |c|
c.save!
CustomerContact.find_or_create_by(customer_id: @customer.id, contact_id: c.id)
end
CustomerContact.where(customer_id: @customer.id, contact_id: contacts_to_delete).destroy_all
format.html { redirect_to edit_customer_path(@customer), success: 'Customer was successfully updated.' }
format.json { render :show, status: :ok, location: @customer }
else
format.html {
(6 - @contacts.count).times { @contacts << @customer.contacts.build }
render :edit
}
format.json { render json: @customer.errors, status: :unprocessable_entity }
end
end
end
def customer_params
params.require(:customer).permit(:company, :name, :line1, :line2, :city, :state, :zipcode, :country, :webaddress, :status,
contacts_attributes: [:fullname, :phone, :email, :department, :manager, :id, :_destroy],
notes_attributes: [:note]
)
end
def contact_params
params.permit(contacts: [:fullname, :email, :phone, :department, :manager, :id, :_destroy])
end