Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BooksController < ApplicationController
- # GET /books
- # GET /books.xml
- before_filter :check_admin, :except => [:index, :show]
- def check_admin
- if !current_user.admin
- redirect_to(books_path, :notice=>"Gotta be admin")
- end
- end
- def index
- #@books = Book.all
- if params[:search]
- @books = Book.search params[:search]
- else
- @books = Book.all
- end
- respond_to do |format|
- format.html # index.html.erb
- format.xml { render :xml => @books }
- end
- end
- # GET /books/1
- # GET /books/1.xml
- def show
- @book = Book.find(params[:id])
- @copies = @book.copies.order(:sold, :price).limit(10)
- 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
- @subject = Subject.all.collect {|s| [ s.name, s.id ]}
- 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])
- @subject = Subject.all.collect {|s| [ s.name, s.id ]}
- end
- # POST /books
- # POST /books.xml
- def create
- @book = Book.new(params[:book])
- @book.user = current_user
- respond_to do |format|
- if @book.save
- format.html { redirect_to(@book, :notice => 'Book was successfully created.') }
- 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])
- format.html { redirect_to(@book, :notice => 'Book was successfully updated.') }
- 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
Add Comment
Please, Sign In to add comment