向关注者发送电子邮件通知

时间:2015-04-08 19:57:56

标签: ruby-on-rails

我想在用户y开始关注用户x

时向用户x发送电子邮件通知

我的跟随控制器中有以下方法:

  class FollowsController < ApplicationController

  before_action :authenticate_user!

  def create
    @user = User.find(params[:id])
    if @user.present?
      current_user.follow!(@user)
   ModelMailer.new_follower_notification(@user).deliver
    end
  end

  def destroy
    @user = User.find(params[:id])
    if @user.present?
      follow = current_user.follows.get_follow(@user).first if current_user.follows
      if follow
        follow.destroy
      end
    end
    respond_to do |format|
      format.js { render :file => "/follows/create", :handlers => [:erb] }
    end
  end   
end

我的模特经纪人:

def new_follower_notification(user)
    @user = user
    mail to: user.email, subject: "#{@user.name}, you are now followd by others.", bcc: "oliviervanhees@gmail.com"
  end

在我的视图文件夹中,我有一个create.js.erb文件

<% if @user %>
  $('#user_<%= @user.id %>').html('<%= escape_javascript(render :partial => 'users/user', :locals => { :user => @user }) %>');
<% end %>

这在我的路线文件中:

  match '/users/:id/follow' => 'follows#create', :as => :user_follow, :via => [:post]
  match '/users/:id/unfollow' => 'follows#destroy', :as => :user_unfollow, :via => [:delete]

我尝试触发模型邮件程序,但是这样做会出错。我想这是因为我在这里使用JSON。有人建议谁来解决这个问题?

0 个答案:

没有答案