将current_user id与导入的联系人列表相关联

时间:2014-11-22 19:16:49

标签: ruby-on-rails

我正在尝试使用基于http://railscasts.com/episodes/396-importing-csv-and-excel的Roo gem将CSV和Excel文件导入rails 4项目(带验证)。

数据被导入数据库,我可以从控制台访问它们。这是模型方法:

class ContactImport

  include ActiveModel::Model
  attr_accessor :file, :user_id

  def initialize(attributes = {})
    attributes.each { |name, value| send("#{name}=", value) }
  end

  def persisted?
    false
  end

  def save
    if imported_contacts.map(&:valid?).all?
      imported_contacts.each(&:save!)
      true
    else
      imported_contacts.each_with_index do |contact, index|
        contact.errors.full_messages.each do |message|
          errors.add :base, "Row #{index+2}: #{message}"
        end
      end
      false
    end
  end

  def imported_contacts
    @imported_contacts ||= load_imported_contacts
  end

  def load_imported_contacts
    spreadsheet = open_spreadsheet
    header = spreadsheet.row(1)
    (2..spreadsheet.last_row).map do |i|
      row = Hash[[header, spreadsheet.row(i)].transpose]
      contact = Contact.find_by_id(row["id"]) || Contact.new
      contact.attributes = row.to_hash.slice('first_name', 'last_name', 'mobile', 'email', 'info')
      contact.user_id = user_id
      contact
    end
  end

  def open_spreadsheet
    case File.extname(file.original_filename)
    when ".csv" then Roo::Csv.new(file.path, nil, :ignore)
    when ".xls" then Roo::Excel.new(file.path, nil, :ignore)
    when ".xlsx" then Roo::Excelx.new(file.path, nil, :ignore)
    else raise "Unknown file type: #{file.original_filename}"
    end
  end
end

控制器看起来像:

class ContactImportsController < ApplicationController
  before_action :authenticate_user!
  def new

    @contact_import = ContactImport.new
  end

  def create
    params[:contact_import].merge({user_id: current_user.id})
    @contact_import = ContactImport.new(contact_params)
    if @contact_import.save
      redirect_to contacts_path, notice: "Imported contacts successfully."
    else
      render :new
    end
  end

  private
  def contact_params
    params.require(:contact_import).permit(:file, :user_id)
  end
end

我不确定如何将current_user id添加到每个联系人。非常感谢任何解决方案。

1 个答案:

答案 0 :(得分:1)

Okey,根据您提供的信息,您可以执行以下操作,将current_user_id添加到您的联系人。

在您的ContactImportController

   .........

  def create
    @contact_import = ContactImport.new(contact_params)
    @contact_import.user_id = current_user.id
    if @contact_import.save
      redirect_to contacts_path, notice: "Imported contacts successfully."
    else
      render :new
    end
  end

  private

  def contact_params
    params.require(:contact_import).permit(:file)
  end

  .........

在您的contact_import.rb文件中

 def load_imported_contacts
   spreadsheet = open_spreadsheet
   header = spreadsheet.row(1)
  (2..spreadsheet.last_row).map do |i|
  row = Hash[[header, spreadsheet.row(i)].transpose]
  contact = Contact.find_by_id(row["id"]) || Contact.new
  contact.attributes = row.to_hash.slice('first_name', 'last_name', 'mobile', 'email', 'info')
  contact.user_id = self.user_id # this should work!
  contact
end

正如您未提及在任何before_save中或在验证回调之前调用Contact#load_imported_contacts的位置。但是我提到current_user.id的方式应该初始化为该联系人模型,同时将数据保存到user_id字段。 我希望这足以解决你的问题。

谢谢!