[RAILS] Schienen CSV Basic

CSV-Ausgabe

<%= link_to "CSV", products_path(format: :csv) %>
  def index
    respond_to do |format|
      format.html do
        @search = Product.ransack(params[:q])
        @products = @search.result    
      end

      format.csv do
        @products = Product.all
        data= CSV.generate do |csv|
          column_names = %w[id title product_price_id]
          csv << column_names
          @products.each do |product|
            row = [product.id, product.title, product.product_price_id]
            csv << row
          end
        end

        send_data(data, filename: 'produsts.csv')
      end
    end
  end

CSV-Upload

gem 'roo'
  resources :products do 
    collection do 
      post :import
    end
  end
  def import
    Product.import(params[:csv_file])
    redirect_to root_path
  end
class Product < ApplicationRecord
  belongs_to :product_price


  def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
      product = Product.new
      product.attributes = row.to_hash.slice(*updatable_attributes)
      product.save
    end
  end

  def self.updatable_attributes
    ["title","product_price_id"]
  end
require 'rails_helper'

RSpec.describe ProductsController, type: :controller do

  describe '#index' do
    before do
      price = ProductPrice.create(price: 2000)
      Product.create(title: "Product1", product_price_id: price.id)
    end

    it 'Ausgabe csv' do
     get :index, format: :csv
     expect(response.body).to include "Product1"
    end

    context 'hochladen' do
      before do
        ProductPrice.create(id:1, price: 2000)
        ProductPrice.create(id:2, price: 2000)
      end
      let(:csv_file) {'test.csv'}

      subject do
        post :import, params: {
          csv_file: fixture_file_upload(csv_file, 'text/csv')
        }  
      end

      it 'aa' do
        expect{subject}.to change(Product,:count).by(2)
      end
    end
  end
end

Recommended Posts

Schienen CSV Basic
Rails Grundlagen
Grundlagen des Rails-Routings
Grundlagen der Rails-Datenbank
Grundlagen des Rails Logger
Rspec Basics [Rails]
Ruby on Rails Grundlagen
[Rails] Einführung in die Grundlagen der Entwicklung
Output Rails Routes als CSV
[Rails] Implementierung der CSV-Importfunktion
[Rails] Implementierung der CSV-Exportfunktion
[Ruby on Rails] CSV-Ausgabefunktion
Implementieren Sie die CSV-Download-Funktion in Rails
[Schienen g. Fehler]
Rails Review 1
Rails API
Schienenmigration
Ruby-Grundlagen
[Rails] first_or_initialize
Schienen Tutry
Fragmentgrundlagen
JPA-Grundlagen 1
Über Schienen 6
Docker-Grundlagen
ViewPager-Grundlagen
Schienenfundament
Rails Memorandum
Schienen Tutorial
Java-Grundlagen
Schienen Tutry
Deshalb habe ich dieses Mal die Methode "Verknüpfen des Inhalts des Verzeichnisses" übernommen. Ich denke, es wird je nach Zeit und Fall richtig verwendet. Tutorial zu Linux, ln, Linux-Befehlsschienen
Java-Grundlagen
Rails Grundlagen zum Erstellen einer neuen Anwendung
JavaScript-Grundlagen
[Schienen] erfinden
Schienen Tutry
JPA-Grundlagen 2
Schienen Tutorial
Java-Grundlagen
Schienen Tipps
Schienenmethode
Schienen Tutorial
RecyclerView-Grundlagen
Rails Review 2
[Rails6 + Vue.js] Implementieren Sie den CSV-Importprozess mithilfe von Axios
So geben Sie die von Rails erstellte CSV in S3 aus
Ruby on Rails ~ Grundlagen von MVC und Router ~