Advertisement
Guest User

Untitled

a guest
Jun 16th, 2015
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. class SubscribersController < ApplicationController
  2. before_action :set_subscriber, only: [:show, :edit, :update, :destroy]
  3.  
  4. respond_to :html
  5.  
  6. def send_welcome
  7. NotificationMailer.welcome(params[:id]).deliver
  8. redirect_to new_subscriber_path
  9. end
  10.  
  11. def index
  12. @subscribers = Subscriber.all
  13. respond_with(@subscribers)
  14. end
  15.  
  16. def show
  17. @subscriber = Subscriber.find(params[:id])
  18. respond_with(@subscriber)
  19. end
  20.  
  21. def new
  22. @subscriber = Subscriber.new
  23. respond_with(@subscriber)
  24. end
  25.  
  26. def edit
  27. @subscriber = Subscriber.find(params[:id])
  28. end
  29.  
  30. def create
  31. @subscriber = Subscriber.new(subscriber_params)
  32. if @subscriber.save
  33. NotificationMailer.welcome(@subscriber)
  34. redirect_to posts_path, notice: "You have successfully subscribed."
  35.  
  36. else
  37. render 'new'
  38. end
  39.  
  40. end
  41.  
  42. def update
  43. @subscriber = Subscriber.find(params[:id])
  44. @subscriber.update(subscriber_params)
  45. respond_with(@subscriber)
  46. end
  47.  
  48. def destroy
  49. @subscriber = Subscriber.find(params[:id])
  50. @subscriber.destroy
  51. respond_with(@subscriber)
  52. end
  53.  
  54. private
  55. def set_subscriber
  56. @subscriber = Subscriber.find(params[:id])
  57. end
  58.  
  59. def set_email
  60. @email = Subscriber.find(params[:email])
  61. end
  62.  
  63. def subscriber_params
  64. params.require(:subscriber).permit(:name, :email, :mobile)
  65. end
  66. end
  67.  
  68. Notification_mailer - Welcome Method
  69.  
  70. class NotificationMailer < ActionMailer::Base
  71. default from: "myemail@gmail.com"
  72.  
  73. # Subject can be set in your I18n file at config/locales/en.yml
  74. # with the following lookup:
  75. #
  76. # en.notification_mailer.welcome.subject
  77. #
  78. def welcome(subscriber)
  79. @subscriber = Subscriber.new(subscriber)
  80. @url = 'http://localhost:3000/posts'
  81. mail :to => "#{@subscriber.name} <#{@subscriber.email}>",
  82. :from => "myemail@gmail.com",
  83. :subject => 'Welcome to My Blog'
  84. end
  85.  
  86. class Subscriber < ActiveRecord::Base
  87.  
  88. validates :name, :email, presence: true
  89. validates :email, uniqueness: true
  90. validates :email, format:
  91. { with: /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})z/i, on: :create }
  92. end
  93.  
  94. <h1>NotificationMailer#welcome</h1>
  95.  
  96. <!DOCTYPE html>
  97. <html>
  98. <head>
  99. <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
  100. </head>
  101. <body>
  102. <h1>Welcome to My Blog, <%= @subscriber.name %></h1>
  103. <p>
  104. You have successfully subscribed! <br>
  105. </p>
  106. <p>
  107. Please visit the site, read the post, and feel free to leave a comment,
  108. just follow this link: <%= @url %>.
  109. </p>
  110. <p>Thanks for joining and have a great day!</p>
  111. </body>
  112. </html>
  113.  
  114. config.action_mailer.delivery_method = :smtp
  115. config.action_mailer.smtp_settings = {
  116. address: 'smtp.gmail.com',
  117. port: 587,
  118. #domain: 'example.com',
  119. user_name: 'myemail@gmail.com',
  120. password: 'Password',
  121. authentication: 'plain',
  122. enable_starttls_auto: true }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement