Guest User

Untitled

a guest
Jul 20th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. class BooksController < ApplicationController
  2. # GET /books
  3. # GET /books.xml
  4.  
  5. before_filter :check_admin, :except => [:index, :show]
  6.  
  7. def check_admin
  8. if !current_user.admin
  9. redirect_to(books_path, :notice=>"Gotta be admin")
  10. end
  11. end
  12.  
  13.  
  14. def index
  15. #@books = Book.all
  16.  
  17. if params[:search]
  18. @books = Book.search params[:search]
  19. else
  20. @books = Book.all
  21. end
  22.  
  23. respond_to do |format|
  24. format.html # index.html.erb
  25. format.xml { render :xml => @books }
  26. end
  27. end
  28.  
  29. # GET /books/1
  30. # GET /books/1.xml
  31. def show
  32. @book = Book.find(params[:id])
  33. @copies = @book.copies.order(:sold, :price).limit(10)
  34.  
  35. respond_to do |format|
  36. format.html # show.html.erb
  37. format.xml { render :xml => @book }
  38. end
  39. end
  40.  
  41. # GET /books/new
  42. # GET /books/new.xml
  43. def new
  44. @book = Book.new
  45. @subject = Subject.all.collect {|s| [ s.name, s.id ]}
  46.  
  47. respond_to do |format|
  48. format.html # new.html.erb
  49. format.xml { render :xml => @book }
  50. end
  51. end
  52.  
  53. # GET /books/1/edit
  54. def edit
  55. @book = Book.find(params[:id])
  56. @subject = Subject.all.collect {|s| [ s.name, s.id ]}
  57. end
  58.  
  59. # POST /books
  60. # POST /books.xml
  61. def create
  62.  
  63. @book = Book.new(params[:book])
  64. @book.user = current_user
  65.  
  66. respond_to do |format|
  67. if @book.save
  68. format.html { redirect_to(@book, :notice => 'Book was successfully created.') }
  69. format.xml { render :xml => @book, :status => :created, :location => @book }
  70. else
  71. format.html { render :action => "new" }
  72. format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
  73. end
  74. end
  75. end
  76.  
  77. # PUT /books/1
  78. # PUT /books/1.xml
  79. def update
  80. @book = Book.find(params[:id])
  81.  
  82. respond_to do |format|
  83. if @book.update_attributes(params[:book])
  84. format.html { redirect_to(@book, :notice => 'Book was successfully updated.') }
  85. format.xml { head :ok }
  86. else
  87. format.html { render :action => "edit" }
  88. format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
  89. end
  90. end
  91. end
  92.  
  93. # DELETE /books/1
  94. # DELETE /books/1.xml
  95. def destroy
  96. @book = Book.find(params[:id])
  97. @book.destroy
  98.  
  99. respond_to do |format|
  100. format.html { redirect_to(books_url) }
  101. format.xml { head :ok }
  102. end
  103. end
  104. end
Add Comment
Please, Sign In to add comment