Advertisement
Guest User

books_controller.rb

a guest
May 6th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. class BooksController < ApplicationController
  2. before_action :set_book, only: [:show, :edit, :update, :destroy]
  3.  
  4. # GET /books
  5. # GET /books.json
  6. def index
  7. @books = Book.all
  8. end
  9.  
  10. # GET /books/1
  11. # GET /books/1.json
  12. def show
  13. end
  14.  
  15. # GET /books/new
  16. def new
  17. @book = Book.new
  18. end
  19.  
  20. # GET /books/1/edit
  21. def edit
  22. end
  23.  
  24. # POST /books
  25. # POST /books.json
  26. def create
  27. puts "Starting"
  28. @book = Book.new(book_params)
  29. @book.chapters.each do |c|
  30. puts c.valid?
  31. end
  32. puts "Ending"
  33.  
  34. respond_to do |format|
  35. if @book.save
  36. format.html { redirect_to @book, notice: 'Book was successfully created.' }
  37. format.json { render :show, status: :created, location: @book }
  38. else
  39. format.html { render :new }
  40. format.json { render json: @book.errors, status: :unprocessable_entity }
  41. end
  42. end
  43. end
  44.  
  45. # PATCH/PUT /books/1
  46. # PATCH/PUT /books/1.json
  47. def update
  48. respond_to do |format|
  49. if @book.update(book_params)
  50. format.html { redirect_to @book, notice: 'Book was successfully updated.' }
  51. format.json { render :show, status: :ok, location: @book }
  52. else
  53. format.html { render :edit }
  54. format.json { render json: @book.errors, status: :unprocessable_entity }
  55. end
  56. end
  57. end
  58.  
  59. # DELETE /books/1
  60. # DELETE /books/1.json
  61. def destroy
  62. @book.destroy
  63. respond_to do |format|
  64. format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
  65. format.json { head :no_content }
  66. end
  67. end
  68.  
  69. private
  70. # Use callbacks to share common setup or constraints between actions.
  71. def set_book
  72. @book = Book.find(params[:id])
  73. end
  74.  
  75. # Only allow a list of trusted parameters through.
  76. def book_params
  77. params.fetch(:book, {}).permit(:title, :release_year,
  78. book_authors_attributes:[:id, :author_name, :_destroy])
  79. end
  80. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement