quarta-feira, 15 de janeiro de 2014

Ruby on Rails-Action Mailer Basics

====
   Based on guides.rubyonrails.org

Action Mailer Basics
    Send eails using mailer classes and views
    Class inherit from ActionMailer::Base and live in app/mailers
    Mailers are conceptually similar to controllers
    Steps
    1. Create the mailer
        $ rails generate mailer UserMailer
        user_mailer.rb created
            class UserMailer < ActionMailer::Base
              default from: 'from@example.com'      <-- br="" default="" from="">            end
    2. It is possible jusst to create the Mailer in app/mailers
        class MyMailer < ActionMailer::Base
        end
    3. Add a method called welcome
         def welcome_email(user)
            @user = user
            @url  = 'http://example.com/login'
            mail(to: @user.email, subject: 'Welcome to My Awesome Site')
         end
    4. Create the views/user_mailer/welcome.html.erb
       
       
         
           
         
         
           

Welcome to example.com, <%= @user.name %>


           
              You have successfully signed up to example.com,
              your username is: <%= @user.login %>.

           

           
              To login to the site, just follow this link: <%= @url %>.
           

            Thanks for joining and have a great day!

         
       
        4.1 It is possible to create a text(non html) file too. welcome_email.text.erb
    With method webcome_email, rails will automatically render both html and text files.
    5. In method create of UsersController
          if @user.save
            # Tell the UserMailer to send a welcome Email after save
            UserMailer.welcome_email(@user).deliver


Nenhum comentário:

Postar um comentário