Я работаю над приложением на основе Ruby on Rails с Bootstrap.Я застрял на бэкэнде.Я не ясно, как вставить некоторые записи в базе данных, которая является активной записью.Я приложил скриншот моих таблиц базы данных, а также мой код.Я надеюсь, что я получу правильный ответ здесь.
Это мой контроллер:
class ImportCsvController < ApplicationController
before_action :require_user
before_action :require_admin
def index
end
# Import CSV file from Sysinflame
def import
end
def import_csv_datei
begin
@import_csvs = FileUploadInfo.new(params[:file])
if @import_csvs.read_save_csv(params[:file])
#Import Successful
user=current_user.user_name
time = Time.now
FileUploadInfo.import_file(params[:file],time.to_formatted_s(time),"erfolgreich",user)
redirect_to :back, notice: "Import Erfolgreich!"
else
#return errors
#format.html {render :index}
#format.json {render json: {status:"Fehler: ",message: "Dateiformat ist nicht erlaubt!"}}
redirect_to :back, notice: "Fehler"
end
#rescue
#redirect_to root_path, notice: "Invalid CSV file format."
end
end
end
теперь вот мой класс модели:
class FileUploadInfo < ActiveRecord::Base
require 'csv'
def initialize(file)
@file = file
end
def read_save_csv(file)
CSV.foreach(file.path, :headers => true) do |row|
file_hash = row.to_hash
end
return true
end
def import_file(file,time,status,user)
end
end
Я застрял в функции import_file и нуждаюсь в некоторых советах здесь.Заранее спасибо!