Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Cannot insert data to table when import file

csv rubyonrails4

  • Please log in to reply
2 replies to this topic

#1 dailammoc

dailammoc

    Dispatcher

  • Members
  • 36 posts
  • Location/

Posted 07 April 2014 - 04:17 AM

I have a table(id, name, price, created_at, updated_at) and a CSV flie with exited data. When I imported the file to the table,only id, created_at and updated_ad were inserted.

SQL (0.6ms)  INSERT INTO `products` (`created_at`, `id`, `updated_at`) VALUES ('2014-04-07 03:16:07', 27, '2014-04-07 03:16:07')

and values of name and price always set to null:

+----+-------+-------+---------------------+---------------------+
| id | name  | price | created_at          | updated_at          |
+----+-------+-------+---------------------+---------------------+
|  7 | fgghg |  5684 | 2014-04-06 04:03:08 | 2014-04-06 04:21:35 |
|  8 | e     |   345 | 2014-04-06 04:11:29 | 2014-04-06 04:11:29 |
|  9 | fd    |    54 | 2014-04-07 02:38:14 | 2014-04-07 02:38:14 |
| 10 | jh    |    66 | 2014-04-07 02:49:23 | 2014-04-07 02:49:23 |
| 29 | NULL  |  NULL | 2014-04-07 03:25:25 | 2014-04-07 03:25:25 |
| 30 | NULL  |  NULL | 2014-04-07 03:25:25 | 2014-04-07 03:25:25 |
| 31 | NULL  |  NULL | 2014-04-07 03:25:25 | 2014-04-07 03:25:25 |
+----+-------+-------+---------------------+---------------------+

this is my controller file:

class ProductsController < ApplicationController
  before_action :set_product, only: [:show, :edit, :update, :destroy]

  # GET /products
  # GET /products.json
  def index
    @products = Product.order(:name)
    respond_to do |format|
      format.html
      format.csv { send_data @products.to_csv }
    end
  end

  # GET /products/1
  # GET /products/1.json
  def show
  end

  # GET /products/new
  def new
    @product = Product.new
  end

  # GET /products/1/edit
  def edit
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(product_params)

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: "Product was successfully created. #{undo_link}" }
        format.json { render action: 'show', status: :created, location: @product }
      else
        format.html { render action: 'new' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /products/1
  # PATCH/PUT /products/1.json
  def update
    respond_to do |format|
      if @product.update(product_params)
        format.html { redirect_to product_url, notice: 'Product was successfully updated.' "#{undo_link}" }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product.destroy
    respond_to do |format|
      format.html { redirect_to products_url, notice: "Successfully destroyed product. #{undo_link}" }
      format.json { head :no_content }
    end
  end

  def import
    Product.import(params[:file])
    redirect_to root_url, notice: "Products imported."
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_product
      @product = Product.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def product_params
      params.require(:product).permit(:name, :price)
    end

    def undo_link
      view_context.link_to("undo", revert_version_path(@product.versions.scoped.last), :method => :post)
    end
end

and this is model file:

class Product < ActiveRecord::Base

  has_paper_trail

  def self.to_csv(options = {})
    CSV.generate(options) do |csv|
      csv << column_names
      all.each do |product|
        csv << product.attributes.values_at(*column_names)
      end
    end
  end
  def self.import(file)
  CSV.foreach(file.path, headers: true) do |row|
    product = find_by_id(row["id"]) || new
    product.attributes = row.to_hash.slice(*column_names)
    product.save!
  end
end
end

I didn't know where is error, so please tell me where I did incorrectly.



#2 Alain ANDRE

Alain ANDRE

    Passenger

  • Members
  • 6 posts
  • LocationBordeaux

Posted 08 April 2014 - 08:42 AM

Are you sure when you look in your browser debugging console that params[:file] has the same fields that Product have ?



#3 dailammoc

dailammoc

    Dispatcher

  • Members
  • 36 posts
  • Location/

Posted 08 April 2014 - 09:08 AM

Are you sure when you look in your browser debugging console that params[:file] has the same fields that Product have ?

I checked it again and saw that params[:file] has the same fields with Product. My project here:

https://github.com/dailammoc1588/store







Also tagged with one or more of these keywords: csv, rubyonrails4

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users