Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

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