PageRenderTime 1328ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/contacts_controller.rb

https://bitbucket.org/elnikov/exam
Ruby | 77 lines | 59 code | 14 blank | 4 comment | 2 complexity | 867177568fa6bae4b8ed4dd3cebe502a MD5 | raw file
  1. class ContactsController < ApplicationController
  2. before_action :set_contact, only: [:show, :edit, :update, :destroy, :share]
  3. skip_before_action :verify_authenticity_token, :only => [:import]
  4. def share
  5. # raise
  6. @contact.share(params[:email])
  7. end
  8. def import
  9. Contact.import(params[:file])
  10. redirect_to contacts_path
  11. end
  12. def index
  13. # @contacts = Contact.all
  14. @q = Contact.ransack(params[:q])
  15. @contacts = @q.result(distinct: true)
  16. respond_to do |format|
  17. format.html
  18. # format.csv { render text: @contacts.to_csv }
  19. format.csv { send_data @contacts.to_csv }
  20. end
  21. end
  22. def show
  23. end
  24. def new
  25. @contact = Contact.new
  26. end
  27. def edit
  28. end
  29. def create
  30. @contact = Contact.new(contact_params)
  31. # raise
  32. respond_to do |format|
  33. if @contact.save
  34. format.html { redirect_to @contact }
  35. else
  36. format.html { render :new }
  37. end
  38. end
  39. end
  40. def update
  41. respond_to do |format|
  42. if @contact.update(contact_params)
  43. format.html { redirect_to @contact }
  44. else
  45. format.html { render :edit }
  46. end
  47. end
  48. end
  49. def destroy
  50. @contact.destroy
  51. respond_to do |format|
  52. format.html { redirect_to contacts_url }
  53. format.js
  54. end
  55. end
  56. private
  57. def set_contact
  58. @contact = Contact.find(params[:id])
  59. end
  60. def contact_params
  61. params.require(:contact).permit(:first_name, :last_name, :email_string, :phone_string)
  62. end
  63. end