[英]rails insert record from one table to another table with matched attributes
I am really rookie in rails. 我真的是菜鸟。 Right now I am doing a project about customers management.
现在,我正在做一个有关客户管理的项目。 Basically, the application manages three phase of a project, sales, quoter, and project.
基本上,应用程序管理项目,销售,报价单和项目的三个阶段。 From contact index page, for any specific contact selected a link_to button should send the customers info to sales table, or quoter table, or project table.
从联系人索引页面中,对于选择的任何特定联系人,link_to按钮应将客户信息发送到销售表,报价单表或项目表。 An alternative way to create new sales, quote, or project is to manually add new item by using Rails CRUD functions.
创建新销售,报价或项目的另一种方法是使用Rails CRUD函数手动添加新项目。 Below is the contact index page, I cut upper-right corner, just let you see how the functions are set up.
下面是联系人索引页面,我在右上角切开了,只是让您了解如何设置功能。
index for contacts table 联系人表索引
I got two problems. 我有两个问题。 First, when I click (Sales/Go)button, show a record successfully created in sales, but all fields are blank with empty value, and in sales index page, a empty row is added on the bottom of the table.Second, I do not know how to add two functions in sales model in 'create' function, one allows user to add contact to sales through contact index page by click go button, another allows user to create new record by using sales new function.
首先,当我单击(Sales / Go)按钮时,显示在销售中成功创建的记录,但是所有字段均为空白且值为空,在Sales Index页面中,表底部添加了一个空行。不知道如何在“创建”功能的销售模型中添加两个功能,一个允许用户通过单击“转到”按钮通过联系人索引页面将联系人添加到销售中,另一个允许用户使用销售新功能创建新记录。
Below is contacts and sales model, control file, routes.rb and contact index. 下面是联系人和销售模型,控制文件,routes.rb和联系人索引。
class Contact < ActiveRecord::Base
has_many :sales,:dependent => :destroy
has_many :quoters,:dependent => :destroy
has_many :projects,:dependent => :destroy
class Sale < ActiveRecord::Base
belongs_to :contact
Quote::Application.routes.draw do
match '/create', :to => 'sales#create/:id' , :as => :create
contacts/index.html.erb 联系人/ index.html.erb
<td><%= link_to raw("Go <span class=\"glyphicon glyphicon-share-alt\"></span>"), sales_path(:contact_id => contact), :class=> "btn btn-xs btn-primary ", method: :post%></td>
if I use below sale model, then I got empty row added in sales table. 如果我使用下面的销售模式,那么我在销售表中添加了空行。
class Sale < ActiveRecord::Base
belongs_to :contact
class SalesController < ApplicationController 类SalesController <ApplicationController
def create
contact = Contact.find(params[:contact_id])
@sale = Sale.create(@contact)
same result if I USE @sale = Sale.new(contact.attributes.slice(:firstName, :lastName)) 如果我使用@sale = Sale.new(contact.attributes.slice(:firstName,:lastName)),则结果相同
If I use alternative below sale model, then I got "Can't mass-assign protected attributes: id, project, created_at, updated_at, project_ID, Social_Media, Website, Ext, Category", another thing I have to clarify here is contact table has different fields with sales table, all the different fields have been listed out by the exception throwing I posted above. 如果我在销售模式下使用其他替代方法,那么我会得到“无法批量分配受保护的属性:ID,项目,created_at,updated_at,project_ID,Social_Media,网站,Ext,类别”,在此我需要澄清的另一件事是联系表我的销售表有不同的字段,我在上面发布的异常抛出列出了所有不同的字段。
class Sale < ActiveRecord::Base
belongs_to :contact
class SalesController < ApplicationController 类SalesController <ApplicationController
def create
contact = Contact.find(params[:contact_id])
@sale = Sale.create(contact.attributes)
Same result if I USE @sale = Sale.create(contact) 如果我使用@sale = Sale.create(contact),结果相同
My environment ruby 1.9.3p392 and rails 3.2.13 我的环境ruby 1.9.3p392和rails 3.2.13
I got a way out, if someone get stuck in same situation, may have some hints. 我有一个出路,如果有人陷入同样的境地,可能会有一些提示。
The ways to handle these two problems: 解决这两个问题的方法:
class SalesController < ApplicationController
def create
unless params[:contact_id].nil?
@contact = Contact.find(params[:contact_id])
@sale = Sale.create(@contact.attributes.slice(*Sale.accessible_attributes))
else
@sale = Sale.new(params[:sale])
end
respond_to do |format|
if @sale.save
format.html { redirect_to @sale, notice: 'Sale was successfully created.' }
format.json { render json: @sale, status: :created, location: @sale }
else
format.html { render action: "new" }
format.json { render json: @sale.errors, status: :unprocessable_entity }
end
end
end
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.