我有一個基本的應用程序,可以爲客戶每個季度收集一次儲蓄數據。有一個視圖顯示每個客戶端的數據與其他帶有圖表和圖表的客戶端數據的比較。我正在尋找一種簡單的方法來實現一個按鈕(或某些東西),使我可以自行決定是否手動將該視圖發送到客戶端的HTML電子郵件中。我只找到了教程,介紹如何在註冊或確認類型的電子郵件時發送電子郵件。在rails應用程序中手動發送特定頁面的電子郵件
0
A
回答
2
# app/mailers/user_mailer.rb
class UserMailer < ActionMailer
def report
@things = Thing.report_stuff
mail :to => '[email protected]', :from => '[email protected]',
:subject => 'that report you want'
end
end
# app/views/user_mailer/report.html.erb
<h1>Report</h1>
<% @things.each do |thing| %>
<%= thing.name %>
<% end %>
# app/controllers/reports_controller.rb
def create
UserMailer.report.deliver
flash[:notice] = 'report sent!'
redirect_to root_path # or wherever
end
# in a view
<% form_tag(reports_path, :method => :post) do %>
<% submit_tag 'send report email' %>
<% end %>
+0
謝謝,這些反應非常好。假設我希望form_tag實際上直接進入特定用戶的「顯示」頁面 - 因爲每個用戶的「顯示」頁面都包含我想要發送電子郵件的報告和圖表,我該如何做? – 2011-04-06 00:04:27
+0
我會將你的演示模板的東西移動到部分(名爲_report.html.erb或類似名稱),那麼你應該能夠在你的郵件視圖中渲染:partial =>'../users/report'並渲染:partial = >'報告'在您的用戶展示頁面上 – Unixmonkey 2011-04-06 00:33:50
你看過這裏嗎?大量的例子:http://guides.rubyonrails.org/action_mailer_basics.html – Ant 2011-04-04 23:48:22