class BooksController < ApplicationController
# GET /books
# GET /books.xml
def index
step=2
@books = Book.paginate(:all, :page=>params[:page], :per_page=>2)
str = ""
datestamp = Date.today.strftime("%d%b%Y%H")
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @books }
format.csv do
csv_data=FasterCSV.generate(str) do |csv|
csv << ["Serial", "Name","Author Name" ,"Description", "pages","isbn", "Created At", "Modified At"]
@books.each do |b|
csv << [b.id,b.name,b.author.name,b.description,b.pages,b.isbn,b.created_at,b.updated_at]
end
end
send_data csv_data,
:type => 'text/csv; charset=iso-8859-1; header=present',
:disposition => "attachment; filename=books"+datestamp+".csv"
flash[:notice] = "Export complete!"
end
end
end
# GET /books/1
# GET /books/1.xml
def show
@book = Book.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @book }
end
end
# GET /books/new
# GET /books/new.xml
def new
@book = Book.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @book }
end
end
# GET /books/1/edit
def edit
@book = Book.find(params[:id])
end
# POST /books
# POST /books.xml
def create
@book = Book.new(params[:book])
respond_to do |format|
if @book.save
flash[:notice] = 'Book was successfully created.'
format.html { redirect_to(@book) }
format.xml { render :xml => @book, :status => :created, :location => @book }
else
format.html { render :action => "new" }
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end
# PUT /books/1
# PUT /books/1.xml
def update
@book = Book.find(params[:id])
respond_to do |format|
if @book.update_attributes(params[:book])
flash[:notice] = 'Book was successfully updated.'
format.html { redirect_to(@book) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /books/1
# DELETE /books/1.xml
def destroy
@book = Book.find(params[:id])
@book.destroy
respond_to do |format|
format.html { redirect_to(books_url) }
format.xml { head :ok }
end
end
end
TechSoft Solutions Interactive Network Blog site. Brings Technology Closer to you and Making IT Work. Randomness, IT geekery and whatever else me and you feel like. TechSoft is also advertising your business for free. Follow TechSoft Solutions for more information.
Thursday, November 24, 2011
How to export CSV in rails 3
Subscribe to:
Posts (Atom)