Last active
December 22, 2015 05:39
-
-
Save carlosfarah/6425144 to your computer and use it in GitHub Desktop.
Sistema de importação de arquivos csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config/application.rb | |
require File.expand_path('../boot', __FILE__) | |
require 'csv' | |
require 'rails/all' | |
Bundler.require(:default, Rails.env) | |
module Import | |
class Application < Rails::Application | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#models/book.rb | |
class Book < ActiveRecord::Base | |
def self.to_csv(options ={}) | |
CSV.generate(options) do |csv| | |
csv << column_names | |
all.each do |book| | |
csv << product.attributes.values_at(*column_names) | |
end | |
end | |
end | |
def self.import(file) | |
CSV.foreach(file.path, headers: true) do |row| | |
Book.create! row.to_hash | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BooksController < ApplicationController | |
before_action :set_book, only: [:show, :edit, :update, :destroy] | |
# GET /books | |
# GET /books.json | |
def index | |
@books = Book.all | |
respond_to do |format| | |
format.html | |
format.csv { send_data @book.to_csv } | |
end | |
end | |
def import | |
Book.import(params[:file]) | |
redirect_to root_url, notice: "Products importados" | |
end | |
# GET /books/1 | |
# GET /books/1.json | |
def show | |
end | |
# GET /books/new | |
def new | |
@book = Book.new | |
end | |
# GET /books/1/edit | |
def edit | |
end | |
# POST /books | |
# POST /books.json | |
def create | |
@book = Book.new(book_params) | |
respond_to do |format| | |
if @book.save | |
format.html { redirect_to @book, notice: 'Book was successfully created.' } | |
format.json { render action: 'show', status: :created, location: @book } | |
else | |
format.html { render action: 'new' } | |
format.json { render json: @book.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# PATCH/PUT /books/1 | |
# PATCH/PUT /books/1.json | |
def update | |
respond_to do |format| | |
if @book.update(book_params) | |
format.html { redirect_to @book, notice: 'Book was successfully updated.' } | |
format.json { head :no_content } | |
else | |
format.html { render action: 'edit' } | |
format.json { render json: @book.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
# DELETE /books/1 | |
# DELETE /books/1.json | |
def destroy | |
@book.destroy | |
respond_to do |format| | |
format.html { redirect_to books_url } | |
format.json { head :no_content } | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_book | |
@book = Book.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def books_params | |
params.require(:books).permit(:title, :author, :description, :isbn13) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#views/books/index.html.erb | |
<h1>Listing books</h1> | |
<table> | |
<thead> | |
<tr> | |
<th>Title</th> | |
<th>Author</th> | |
<th>Description</th> | |
<th></th> | |
<th></th> | |
<th></th> | |
</tr> | |
</thead> | |
<tbody> | |
<% @books.each do |book| %> | |
<tr> | |
<td><%= book.title %></td> | |
<td><%= book.author %></td> | |
<td><%= book.description %></td> | |
<td><%= link_to 'Show', book %></td> | |
<td><%= link_to 'Edit', edit_book_path(book) %></td> | |
<td><%= link_to 'Destroy', book, method: :delete, data: { confirm: 'Are you sure?' } %></td> | |
</tr> | |
<% end %> | |
</tbody> | |
</table> | |
<br> | |
<h2>Importar Livros</h2> | |
<%= form_tag import_books_path, multipart: true do %> | |
<%= file_field_tag :file %> | |
<%= submit_tag "Import" %> | |
<% end %> | |
<%= link_to 'New Book', new_book_path %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#config/routs.rb | |
Import::Application.routes.draw do | |
resources :books do | |
collection { post :import } | |
end | |
resources :products | |
resources :distribuidors | |
root to: 'books#index' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
isbn13 | title | author | description | |
---|---|---|---|---|
111111 | Ruby on Rails | Vinícius Baggio Fuentes | Coloque sua aplicação web nos trilhos | |
222222 | Use a cabeça PHP & MySQL | Beighley & Morrison | Aprenda a desenvolver PHP usando MySQL |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment