You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

81 lines
2.1 KiB

  1. class DocumentsController < ApplicationController
  2. before_action :set_document, only: [:show, :edit, :update, :destroy]
  3. # GET /documents
  4. # GET /documents.json
  5. def index
  6. @documents = User.find(session[:user_id]).documents
  7. end
  8. # GET /documents/1
  9. # GET /documents/1.json
  10. def show
  11. end
  12. # GET /documents/new
  13. def new
  14. @document = Document.new
  15. end
  16. # GET /documents/1/edit
  17. def edit
  18. end
  19. # POST /documents
  20. # POST /documents.json
  21. def create
  22. @document = Document.new(document_params)
  23. @document.name = @document.doc_file_name
  24. @document.user = User.find(session[:user_id])
  25. if Document.exists?(doc_fingerprint: @document.doc_fingerprint)
  26. end
  27. respond_to do |format|
  28. if @document.save
  29. format.html { redirect_to @document, notice: 'Document was successfully created.' }
  30. format.json { render :show, status: :created, location: @document }
  31. else
  32. format.html { render :new }
  33. format.json { render json: @document.errors, status: :unprocessable_entity }
  34. end
  35. end
  36. end
  37. # PATCH/PUT /documents/1
  38. # PATCH/PUT /documents/1.json
  39. def update
  40. respond_to do |format|
  41. if @document.update(document_params)
  42. format.html { redirect_to @document, notice: 'Document was successfully updated.' }
  43. format.json { render :show, status: :ok, location: @document }
  44. else
  45. format.html { render :edit }
  46. format.json { render json: @document.errors, status: :unprocessable_entity }
  47. end
  48. end
  49. end
  50. # DELETE /documents/1
  51. # DELETE /documents/1.json
  52. def destroy
  53. @document.destroy
  54. respond_to do |format|
  55. format.html { redirect_to documents_url, notice: 'Document was successfully destroyed.' }
  56. format.json { head :no_content }
  57. end
  58. end
  59. private
  60. # Use callbacks to share common setup or constraints between actions.
  61. def set_document
  62. @document = Document.find(params[:id])
  63. end
  64. # Never trust parameters from the scary internet, only allow the white list through.
  65. def document_params
  66. params.require(:document).permit(:doc, :category_id)
  67. end
  68. end