PageRenderTime 1311ms CodeModel.GetById 49ms RepoModel.GetById 1ms app.codeStats 0ms

/app/controllers/books_controller.rb

https://gitlab.com/igorduarte/Library
Ruby | 77 lines | 52 code | 10 blank | 15 comment | 2 complexity | 4d7cc725cb8db6090229fb72d7cbb441 MD5 | raw file
  1. class BooksController < ApplicationController
  2. before_action :set_book, only: [:show, :edit, :update, :destroy]
  3. before_action :authenticate_user!
  4. # GET /books
  5. # GET /books.json
  6. def index
  7. #@books = Book.all
  8. @q = Book.ransack(params[:q])
  9. @books = @q.result
  10. end
  11. # GET /books/1
  12. # GET /books/1.json
  13. def show
  14. end
  15. # GET /books/new
  16. def new
  17. @book = Book.new
  18. end
  19. # GET /books/1/edit
  20. def edit
  21. end
  22. # POST /books
  23. # POST /books.json
  24. def create
  25. @book = Book.new(book_params)
  26. respond_to do |format|
  27. if @book.save
  28. format.html { redirect_to @book, notice: 'Book was successfully created.' }
  29. format.json { render :show, status: :created, location: @book }
  30. else
  31. format.html { render :new }
  32. format.json { render json: @book.errors, status: :unprocessable_entity }
  33. end
  34. end
  35. end
  36. # PATCH/PUT /books/1
  37. # PATCH/PUT /books/1.json
  38. def update
  39. respond_to do |format|
  40. if @book.update(book_params)
  41. format.html { redirect_to @book, notice: 'Book was successfully updated.' }
  42. format.json { render :show, status: :ok, location: @book }
  43. else
  44. format.html { render :edit }
  45. format.json { render json: @book.errors, status: :unprocessable_entity }
  46. end
  47. end
  48. end
  49. # DELETE /books/1
  50. # DELETE /books/1.json
  51. def destroy
  52. @book.destroy
  53. respond_to do |format|
  54. format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
  55. format.json { head :no_content }
  56. end
  57. end
  58. private
  59. # Use callbacks to share common setup or constraints between actions.
  60. def set_book
  61. @book = Book.find(params[:id])
  62. end
  63. # Never trust parameters from the scary internet, only allow the white list through.
  64. def book_params
  65. params.require(:book).permit(:name, :id_publisher, :id_author, :synopsis, :pages, :releaseYear, :language)
  66. end
  67. end