2015-02-10 9 views
1

我在我的應用程序中設置了動作郵件程序設置,以便在微博上發送新評論時向微博用戶發送郵件。對新評論通知欄使用動作郵件程序4

這裏是我的代碼

comments_controller

def create 
    @micropost = Micropost.find(params[:micropost_id]) 
    @comment = Comment.new(params[:comment].permit(:user_id, :body, :micropost_id)) 
    @comment.micropost = @micropost 
    @comment.micropost.user = @micropost.user 
    @comment.user = current_user 

    if @comment.save 
    UserMailer.newcomment_email(@comment).deliver 
    @comment.create_activity :create, owner: current_user 
    flash[:success] = "Comment created!" 
    redirect_to @micropost 
    else 
    render 'shared/_comment_form' 
    end 
end 

user_mailer.rb

class UserMailer < ActionMailer::Base 
    default from: "[email protected]" 

    def newcomment_email(comment) 
    @comment = comment 

    mail(to: comment.micropost.user.email, subject: "New Comment Notify") 
    end 
end 

newcomment_email.html.erb

<%= @comment.user.username %> commented on your post <%= "micropost link?" %> 
<p>comment: <%= @comment.body %></p> 

該代碼工作正常,併發送micropost用戶郵件通知說用戶評論您的帖子。那麼我怎麼把微博鏈接放在電子郵件中呢?

回答

1

在您的郵件地址爲newcomment_email.html.erb的視圖中,將link_to標記添加到評論中。例如:

<%= link_to 'micropost', micropost_url(@comment.micropost) %> 

這只是一個例子。根據您的應用程序更改它。它會做必要的。希望這可以幫助。

+1

的存在微柱的OP正在尋找鏈接微柱,而不是評論鏈接。我認爲你正在尋找'link_to'micropost',micropost_url(@ comment.micropost)'。 – 2015-02-10 01:02:11

+0

是的。這就是爲什麼我提到這只是一個例子。相應地更改它。但是,謝謝我會編輯它。 :) – Deep 2015-02-10 01:04:04

+0

@Deep當我有一個關係控制器'def創建 @user = User.find(params [:relationship] [:followed_id])時,我該如何爲新的跟隨通知做這個 current_user.follow!(@ user ) respond_to do | format | format.html {redirect_to的@user} format.js 結束 結束',將是我的代碼在我usermailer.rb – Day 2015-02-17 09:25:10

0

然後加在你的routes.rb

Your::Application.routes.draw do 

default_url_options :host => "www.example.com" 

end 

將您的應用程序域名將用戶重定向到該網站上

+0

tanx深。欣賞它 – Day 2015-02-10 14:08:00

相關問題