1. # users_controller.rb
  2. class UsersController < ApplicationController
  3. before_action :set_user, only: [:show, :edit, :update, :destroy]
  4. def index
  5. @users = User.all
  6. end
  7. def show
  8. end
  9. def new
  10. @user = User.new
  11. end
  12. def edit
  13. end
  14. def create
  15. @user = User.new(user_params)
  16. respond_to do |format|
  17. if @user.save
  18. # Tell the UserMailer to send a welcome email after save
  19. UserMailer.with(user: @user).remind_email.deliver_later(wait_until: @user.date_and_time)
  20. format.html { redirect_to root_path }
  21. format.json { render :show, status: :created, location: @user }
  22. else
  23. format.html { render :new }
  24. format.json { render json: @user.errors, status: :unprocessable_entity }
  25. end
  26. end
  27. end
  28. def destroy
  29. @user.destroy
  30. respond_to do |format|
  31. format.html { redirect_to users_url}
  32. format.json { head :no_content }
  33. end
  34. end
  35. private
  36. def set_user
  37. @user = User.find(params[:id])
  38. end
  39. def user_params
  40. params.require(:user).permit(:name, :email, :note, :datetime)
  41. end
  42. end
  43. ------------------------------------------------------------------------------------------
  44. #user_mailer.rb
  45. class UserMailer < ApplicationMailer
  46. default from: '[email protected]'
  47. def remind_email
  48. @user = params[:user]
  49. mail(to: @user.email, subject: "Sent from Mailme" )
  50. end
  51. end
  52. ------------------------------------------------------------------------------------------
  53. #new.html.erb
  54. <div class="container">
  55. <div class="can_use">
  56. <div class="row">
  57. <div class="d-none d-sm-block col-sm-12 col-md-12 col-lg-12 col-xl-12">
  58. <p class="text-center" style="font-size: 24px;"><span>Remind yourself</span> Remind your friend</p>
  59. </div>
  60. </div>
  61. <div class="d-block d-sm-none col-xs-16">
  62. <br>
  63. <p class="text-center" style="font-size: 20px;"><span>Remind yourself</span> Remind your friend</p>
  64. <br>
  65. </div>
  66. <%= form_for :user,url: users_path, :html => {:class => "form-horizontal center"} do |f| %>
  67. <% if @user && @user.errors.any? %>
  68. <p>
  69. <ul>
  70. <% @user.errors.full_messages.each do |msg| %>
  71. <li><%= msg %></li>
  72. <% end %>
  73. </ul>
  74. </p>
  75. <% end %>
  76. <div class="form-group">
  77. <%= f.text_field :name, class: "form-control", placeholder: "Your name" %>
  78. </div>
  79. <div class="form-group">
  80. <%= f.email_field :email, class: "form-control", placeholder: "Email of reciever" %>
  81. </>
  82. </div>
  83. <div class="form-group">
  84. <%= f.datetime_field :date_and_time, id: "datetimepicker", class: "form-control", placeholder: "Date and Time for remind" %>
  85. </div>
  86. <div class="form-group">
  87. <%= f.text_area :note, class: "form-control", rows: "5" , placeholder: "Note" %>
  88. </div>
  89. <%= f.submit 'Remind Me', class: "btn btn-lg btn-primary btn-block" %>
  90. <% end %>
  91. </div>
  92. </div>
  93. ------------------------------------------------------------------------------------------
  94. #user_job.rb
  95. class UserJob < ApplicationJob
  96. queue_as :default
  97. def perform(*args)
  98. UserMailer.remind_email(user).deliver_later
  99. end
  100. end
  101. ------------------------------------------------------------------------------------------
  102. #schema.rb
  103. ActiveRecord::Schema.define(version: 2019_08_29_005204) do
  104. # These are extensions that must be enabled in order to support this database
  105. enable_extension "plpgsql"
  106. create_table "guests", force: :cascade do |t|
  107. t.text "guest_name"
  108. t.text "guest_email"
  109. t.text "guest_message"
  110. t.datetime "created_at", null: false
  111. t.datetime "updated_at", null: false
  112. end
  113. create_table "users", force: :cascade do |t|
  114. t.text "name"
  115. t.text "email"
  116. t.text "note"
  117. t.datetime "date_and_time"
  118. t.datetime "created_at", null: false
  119. t.datetime "updated_at", null: false
  120. end
  121. end
Comments powered by Disqus