在link_to语句中使用ActionMailer方法 - Rails 3.2.3

时间:2012-05-24 14:55:41

标签: actionmailer ruby-on-rails-3.2

我想在点击时提供链接,它会向管理员发送电子邮件通知他们删除记录。我搜索了几个小时,包括在这里,但找不到任何例子。

以下是我想要修改的当前代码:

link_to "Delete your User Account?", @user, method: :delete, confirm: "You sure? This will permanently delete your account.  If you want to access our site in the future you will need to create a new account again.  Are you sure you want to delete your account?" %>

我想用ActionMailer方法替换“method :: delete”。我查看了api.rubyonrails.org上的Ruby文档和其他地方的示例,但找不到任何内容。

这是我可以在不安装宝石的情况下做的事情吗?

为什么找到带有示例的文档如此困难?叹息.....

1 个答案:

答案 0 :(得分:0)

“method:delete”是调用url时在头文件中传递的HTTP方法,然后路由使用它来查找正确的控制器和操作。

您需要将方法调用发送到控制器中的用户#destroy action is。

会是这样的。

def destroy
  @user = User.find(params[:user])
  if @user.desotry
    # Tell the UserMailer to send a email after destroy
    UserMailer.delete_account_email(@user).deliver
    redirect_to(@user, :notice => 'Account was successfully closed.')
  end
end