简体   繁体   English

rails将记录从一个表插入到具有匹配属性的另一个表

[英]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.

相关问题 将一个表记录复制到rails中 - Copy one table record to another in rails 如何在Rails 3中将数据从一个表插入到另一个表 - How to Insert data from one table to another in Rails 3 当一个表的一个记录由Rails从nil变为notn时,如何保存到另一表中? - How to save into another table when one table's one record changed from nil to not nil with Rails? 从另一个表访问属性-Rails - Access attributes from another table - rails Rails-在表单提交时用于将属性中的记录从一个表复制到另一个表的语法 - Rails - syntax for copying a record in attribute from one table to another table on form submission 仅当数据与Rails中从一个表到另一个表的特定条件匹配时,才如何插入数据? - How do I insert data only if it matches a certain condition from one table to another table in Rails? Rails 4-如果在另一个表中不是特定记录,如何从一个表中选择数据? - Rails 4 - How to select data from a table if in another table is not a specific record? 在Rails中创建具有一个模型和另一个模型上的两个属性的连接表 - Creating Join Table in Rails with One Model and Two Attributes on Another Model Ruby on Rails:如何通过用户界面中的按钮将特定/选择的记录从一个表(用户)发送到另一个表 - Ruby on Rails: How to send a specific/chosen record from one table (User) to another with a button from user interface Rails将记录复制到另一个表 - Rails copy a record to another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM