[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 @   Zhentiw  阅读(488)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示