[Rails Level 2] Mailer, Pipline

GENERATE MAILER

Enter the command for generating a mailer called WeaponMailer which has the emails low_ammo andbroken.

rails g mailer WeaponMailer boken low_ammo

 

SIMPLE MAILER

Code up the low_ammo mailer with the subject of "#{weapon.name} has low ammo", the email should be sent to the zombie.email. Lastly, set the default from address for all emails in WeaponMailer toadmin@rfz.com.

class WeaponMailer < ActionMailer::Base
  default from: "admin@rfz.com"
  
  def low_ammo(weapon, zombie) 
  
    mail to: zombie.email, subject: "#{weapon.name} has low ammo"
  end
end

 

MAIL DELIVERY

Finish coding the check_ammo method on the Weapon model so when we have exactly three ammo left, it will send out the low_ammo mailer we just created.

class WeaponMailer < ActionMailer::Base
  default from: "admin@rfz.com"
        
  def low_ammo(weapon, zombie)
    mail to: zombie.email, subject: "#{weapon.name} has low ammo"
  end 
end

Answer:

Read More: http://guides.rubyonrails.org/action_mailer_basics.html

class Weapon < ActiveRecord::Base
  belongs_to :zombie 

  before_save :check_ammo

  def check_ammo
    WeaponMailer.low_ammo(self, self.zombie).deliver if ammo == 3
  end
end

 

ATTACHING A FILE

Change the low_ammo method to include a picture of the weapon that's low on ammo as an attachment. You can name the file weapon.jpg and load the file using weapon.picture_file.

class WeaponMailer < ActionMailer::Base
  default from: "admin@rfz.com"

  def low_ammo(weapon, zombie)
    attachments['weapon.jpg'] = weapon.picture_file
    mail to: zombie.email, subject: "#{weapon.name} has low ammo"
  end 
end

 

ASSET TAGS

Convert the following to their appropriate asset tags.

<%= image_tag "weapon.png" %>
<%= javascript_include_tag "weapon" %>
<%= stylesheet_link_tag "weapon" %>

 

ASSET PATH

Convert the following scss.erb file to properly reference the asset_path for the image listed in it. Also, try refactoring the scss to use nesting.

h2#newUser {
  text-indent: -9999px; 
  a {
    height: 64px;
    width: 50px;
    display: block;
     background: url(<%= asset_path("rails.png")%>) no-repeat;
  }
}

 

COFFEESCRIPTIN

Use CoffeeScript so when the New Weapon link is pressed it makes the #newWeapon div visible and then hides the New Weapon link. Don't forget to call preventDefault().

$(document).ready ->
  $("#displayWeaponForm").click (event) ->
    event.preventDefault()
    $(this).hide()
    $("#newWeapon").show()

 

REQUIRING JAVASCRIPTS

Modify the application.js below adding the calendar.js and color_picker.js libraries after jquery_ujs.

app/assets/javascripts/applications.js

//= require jquery
//= require jquery_ujs 
//= require calendar
//= require color_picker
//= require_tree .

 

posted @ 2014-09-27 20:49  Zhentiw  阅读(493)  评论(0)    收藏  举报