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

contact_messages_controller.rb 3.2KB

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