Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SubscribersController < ApplicationController
- before_action :set_subscriber, only: [:show, :edit, :update, :destroy]
- respond_to :html
- def send_welcome
- NotificationMailer.welcome(params[:id]).deliver
- redirect_to new_subscriber_path
- end
- def index
- @subscribers = Subscriber.all
- respond_with(@subscribers)
- end
- def show
- @subscriber = Subscriber.find(params[:id])
- respond_with(@subscriber)
- end
- def new
- @subscriber = Subscriber.new
- respond_with(@subscriber)
- end
- def edit
- @subscriber = Subscriber.find(params[:id])
- end
- def create
- @subscriber = Subscriber.new(subscriber_params)
- if @subscriber.save
- NotificationMailer.welcome(@subscriber)
- redirect_to posts_path, notice: "You have successfully subscribed."
- else
- render 'new'
- end
- end
- def update
- @subscriber = Subscriber.find(params[:id])
- @subscriber.update(subscriber_params)
- respond_with(@subscriber)
- end
- def destroy
- @subscriber = Subscriber.find(params[:id])
- @subscriber.destroy
- respond_with(@subscriber)
- end
- private
- def set_subscriber
- @subscriber = Subscriber.find(params[:id])
- end
- def set_email
- @email = Subscriber.find(params[:email])
- end
- def subscriber_params
- params.require(:subscriber).permit(:name, :email, :mobile)
- end
- end
- Notification_mailer - Welcome Method
- class NotificationMailer < ActionMailer::Base
- default from: "myemail@gmail.com"
- # Subject can be set in your I18n file at config/locales/en.yml
- # with the following lookup:
- #
- # en.notification_mailer.welcome.subject
- #
- def welcome(subscriber)
- @subscriber = Subscriber.new(subscriber)
- @url = 'http://localhost:3000/posts'
- mail :to => "#{@subscriber.name} <#{@subscriber.email}>",
- :from => "myemail@gmail.com",
- :subject => 'Welcome to My Blog'
- end
- class Subscriber < ActiveRecord::Base
- validates :name, :email, presence: true
- validates :email, uniqueness: true
- validates :email, format:
- { with: /A([^@s]+)@((?:[-a-z0-9]+.)+[a-z]{2,})z/i, on: :create }
- end
- <h1>NotificationMailer#welcome</h1>
- <!DOCTYPE html>
- <html>
- <head>
- <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
- </head>
- <body>
- <h1>Welcome to My Blog, <%= @subscriber.name %></h1>
- <p>
- You have successfully subscribed! <br>
- </p>
- <p>
- Please visit the site, read the post, and feel free to leave a comment,
- just follow this link: <%= @url %>.
- </p>
- <p>Thanks for joining and have a great day!</p>
- </body>
- </html>
- config.action_mailer.delivery_method = :smtp
- config.action_mailer.smtp_settings = {
- address: 'smtp.gmail.com',
- port: 587,
- #domain: 'example.com',
- user_name: 'myemail@gmail.com',
- password: 'Password',
- authentication: 'plain',
- enable_starttls_auto: true }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement