【问题标题】:Sending an email notification on button click单击按钮发送电子邮件通知
【发布时间】:2013-04-07 16:33:36
【问题描述】:

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

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

我是否以正确的方式处理这件事,还是我走错了路?非常感谢任何帮助!

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

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

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

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

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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3.2


    【解决方案1】:

    其他人在另一个网站上为我回答了这个问题,我以错误的方式处理它。代码如下:

    /app/controllers/pins_controller.rb:

    ...  
    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  
    ...
    

    【讨论】:

      【解决方案2】:

      您可以进行单独的控制器操作来通知 pin 所有者,并让链接使用 jquery 提交请求,然后不需要加载页面,并且您也可以让 jquery 处理已发送的通知。

      我根本不知道你的代码库,所以这是一个粗略的例子

      # Routs file
      get "/pins/:id/post" => "pins#notify", as: notify_pin_owner
      
      # Controller
      def notify
      @pin = Pin.find(params[:id])
      <% if user_signed_in? %>
      <% UserMailer.request_pin(@users, @pin).deliver %>
      <p>
      Your request has been sent!
      </p>
      <% else %>
      ...
      <% end %>
      end
      
      # View
      <%= link_to "notify", "#", data-link: notify_pin_owner_path( @pin ), class: 'pin-notification' %>
      
      # In javascript
      $(document).ready( function() {
        $('.pin-notification').click( function() {
          $.ajax({
            type: "GET",
            url: $(this).data('link'),
            success: 
          })
        })
      })
      

      希望有帮助

      【讨论】:

        猜你喜欢
        • 2011-08-04
        • 1970-01-01
        • 2011-11-14
        • 2012-03-10
        • 1970-01-01
        • 2016-05-21
        • 1970-01-01
        • 1970-01-01
        • 2012-05-02
        相关资源
        最近更新 更多