单击按钮时发送电子邮件通知 [英] Sending an email notification on button click

查看:64
本文介绍了单击按钮时发送电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的任何可用于借出/出售的 Pin 图显示页面上创建一个按钮,点击该按钮后,会向 Pin 图的所有者发送一封电子邮件,让他们知道 current_user 对该项目感兴趣,并且然后发出通知让 current_user 知道电子邮件已发送...

I would like to create a button on any of my pin show pages that are available for loan/sale that, when clicked, sends an email to the owner of the pin letting them know that the current_user is interested in the item and then giving a notice to let the current_user know that the email was sent...

我已经设置了整个电子邮件部分,并且能够让它在页面加载时发送电子邮件,但我希望它仅在单击按钮时发送.看起来我必须让按钮加载一个既发送电子邮件又显示确认的页面.我遇到的问题是将 @pin 和 current_user 变量传递到页面中.

I have set up the whole email part and was able to get it to send an email on loading of the page, but I want it to only send when the button is clicked. It is looking like I will have to make the button load a page that both sends the email and displays confirmation. The problem I am having with that is getting the @pin and current_user variables to be passed into the page.

我是在以正确的方式解决这个问题,还是我偏离了方向?非常感谢任何帮助!

Am I going about this in the right way, or am I way off? Any help is greatly appreciated!

这是我打开发送/确认页面的方式:

Here is how I am opening the send/confirm page:

<%= button_tag(:type => 'button', class: "btn btn-primary", onclick: "window.location.href='/sendrequest'") do %>
<%= content_tag(:strong, 'Request Contact') %>
<% end %>

这是我需要在该页面上执行的操作:

And here is what I need to execute on that page:

<% if user_signed_in? %>
<% UserMailer.request_pin(@users, @pin).deliver %>
<p>
Your request has been sent!
</p>
<% else %>
...
<% end %>

UserMailer.request_pin 中的所有代码都运行良好.

All of the code in UserMailer.request_pin is working fine.

推荐答案

其他人在其他网站上为我回答了这个问题,我的做法是错误的.代码如下:

Someone else has answered this for me on a different site, I was going about it in the wrong way. Here is the code:

...  
def sendrequest  
  @user = current_user  
  @pin = Pin.find(params[:id])  #The culprit!  
  if user_signed_in?  
    UserMailer.request_pin(current_user, @pin).deliver  
    redirect_to @pin, notice: 'Request for contact sent.'  
  else  
  end  
end  
...  

/app/mailer/user_mailer.rb:

class UserMailer < ActionMailer::Base  
  default :from => "ian@ctrl-r.com"  

  def request_pin(user, pin)  
    @user = user  
    @pin = pin  
    mail(:to => "#{@pin.user.name} <#{@pin.user.email}>", :replyto => @user.email, :subject => "#{@user.name} has requested #{@pin.description}")  
  end  
end  

/app/pins/show.html.erb:

...  
<%= link_to "Request Contact", sendrequest_pin_path(current_user, @pin), class: "btn btn-primary" %>  
...  

/config/routes.rb:

...  
resources :pins do  
  resources :loans  
  member do  
    match 'sendrequest' => 'pins#sendrequest'  
  end  
end  
...

这篇关于单击按钮时发送电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆