首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Action mailer进行新的注释通知rails 4

使用Action mailer进行新的注释通知rails 4
EN

Stack Overflow用户
提问于 2015-02-10 00:45:00
回答 2查看 427关注 0票数 1

我已经在我的应用程序中设置了行动邮件发送给微博用户一封邮件,每当一个新的评论已经在他的微博上。

这是我的密码

comments_controller

代码语言:javascript
复制
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

代码语言:javascript
复制
class UserMailer < ActionMailer::Base
  default from: "deppy007@gmail.com"

  def newcomment_email(comment)
    @comment = comment

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

newcomment_email.html.erb

代码语言:javascript
复制
<%= @comment.user.username %> commented on your post <%= "micropost link?" %>
<p>comment: <%= @comment.body %></p> 

代码运行良好,并向微博用户发送邮件提示,表示用户对您的帖子进行了评论。那么我该如何将微博链接放到电子邮件中呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-10 00:53:55

在您的邮件程序(即newcomment_email.html.erb )的视图中,向注释添加一个link_to标记。例如:

代码语言:javascript
复制
<%= link_to 'micropost', micropost_url(@comment.micropost) %>

这只是一个例子。根据您的应用程序更改它。它就能达到要求。希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2015-02-10 14:02:48

然后添加您的routes.rb

代码语言:javascript
复制
Your::Application.routes.draw do

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

end

添加应用程序域名将使用户重定向到存在于微博上的站点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28422222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档