A website template with lots of features, built with ruby on rails.

contact_messages_controller.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. class ContactMessagesController < ApplicationController
  2. before_action :set_contact_message, only: [:show, :edit, :update, :destroy, :unread, :readed]
  3. # GET /contact_messages
  4. # GET /contact_messages.json
  5. def index
  6. @contact_message = ContactMessage.new
  7. end
  8. # POST /contact_messages
  9. # POST /contact_messages.json
  10. def create
  11. @contact_message = ContactMessage.new(contact_message_params)
  12. if user_signed_in?
  13. @contact_message.user = current_user
  14. end
  15. @contact_message.unread = true
  16. respond_to do |format|
  17. if @contact_message.save
  18. UserMailer.contact_message(@contact_message).deliver
  19. format.html { redirect_to contact_messages_path, notice: (t 'contact.delivered') }
  20. format.json { render action: 'show', status: :created, location: @contact_message }
  21. else
  22. format.html { redirect_to contact_path, alert: (t 'contact.not_delivered') }
  23. format.json { render json: @contact_message.errors, status: :unprocessable_entity }
  24. end
  25. end
  26. end
  27. def show
  28. @contact_message.unread = false
  29. @contact_message.save
  30. end
  31. def readed
  32. @contact_message.unread = false
  33. respond_to do |format|
  34. if @contact_message.save
  35. format.html { redirect_to admin_contact_messages_path, notice: 'Contact message marked as readed.' }
  36. format.json { render action: 'show', status: :created, location: @contact_message }
  37. else
  38. format.html { render action: 'new' }
  39. format.json { render json: @contact_message.errors, status: :unprocessable_entity }
  40. end
  41. end
  42. end
  43. def unread
  44. @contact_message.unread = true
  45. respond_to do |format|
  46. if @contact_message.save
  47. format.html { redirect_to admin_contact_messages_path, notice: 'Contact message marked as unread.' }
  48. format.json { render action: 'show', status: :created, location: @contact_message }
  49. else
  50. format.html { render action: 'new' }
  51. format.json { render json: @contact_message.errors, status: :unprocessable_entity }
  52. end
  53. end
  54. end
  55. # PATCH/PUT /contact_messages/1
  56. # PATCH/PUT /contact_messages/1.json
  57. def update
  58. respond_to do |format|
  59. if @contact_message.update(contact_message_params)
  60. format.html { redirect_to @contact_message, notice: 'Contact message was successfully updated.' }
  61. format.json { head :no_content }
  62. else
  63. format.html { render action: 'edit' }
  64. format.json { render json: @contact_message.errors, status: :unprocessable_entity }
  65. end
  66. end
  67. end
  68. # DELETE /contact_messages/1
  69. # DELETE /contact_messages/1.json
  70. def destroy
  71. @contact_message.destroy
  72. respond_to do |format|
  73. format.html { redirect_to contact_messages_url }
  74. format.json { head :no_content }
  75. end
  76. end
  77. private
  78. # Use callbacks to share common setup or constraints between actions.
  79. def set_contact_message
  80. @contact_message = ContactMessage.find(params[:id])
  81. end
  82. # Never trust parameters from the scary internet, only allow the white list through.
  83. def contact_message_params
  84. params.require(:contact_message).permit(:title, :email, :content, :unread, :user_id)
  85. end
  86. end