Rails CSV basics

csv output

<%= 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 'output csv' do
     get :index, format: :csv
     expect(response.body).to include "Product1"
    end

    context 'upload' 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

Rails CSV basics
Rails basics
Rails Routing Basics
Rails database basics
Rails Logger Basics
Rspec Basics [Rails]
Ruby on Rails basics
[Rails] Introduction of devise Basics
Output Rails Routes as csv
[Rails] Implementation of CSV import function
[Rails] Implementation of CSV export function
[Ruby on Rails] CSV output function
Implement CSV download function in Rails
[Rails g.error]
Rails Review 1
Rails API
Rails migration
Ruby basics
Ruby basics
[Rails] first_or_initialize
rails tutorial
Fragment basics
JPA Basics 1
About Rails 6
Docker basics
ViewPager basics
Rails foundation
Rails memorandum
rails tutorial
Java basics
rails tutorial
rails tutorial
Java basics
RSpec Basics
Rails Basics of creating a new application
JavaScript basics
[Rails] devise
rails tutorial
JPA Basics 2
rails tutorial
Hash basics
Java basics
Rails Tips
rails method
rails tutorial
Ruby basics
[Rails] ActiveRecord
[Rails] form_with
RecyclerView Basics
Rails Review 2
[Rails6 + Vue.js] Implement CSV import process using axios
How to output CSV created by Rails to S3
Ruby on Rails ~ Basics of MVC and Router ~