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

contact_messages_controller.rb 3.1KB

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