简体   繁体   中英

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. An alternative way to create new sales, quote, or project is to manually add new item by using Rails CRUD functions. 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.

Below is contacts and sales model, control file, routes.rb and contact index.

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

<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

  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))

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.

class Sale < ActiveRecord::Base

    belongs_to :contact

class SalesController < ApplicationController

  def create

    contact =  Contact.find(params[:contact_id])

    @sale = Sale.create(contact.attributes)

Same result if I USE @sale = Sale.create(contact)

My environment ruby 1.9.3p392 and 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM