2013-04-07 146 views
0

我想創建一個按鈕在我的任何可用於出售/出售的銷售頁面上,點擊後,發送一封電子郵件給銷主,讓他們知道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代碼工作正常。

回答

2

別人已經在不同的網站上回答了這個對我來說,我繞了以錯誤的方式。下面是代碼:

/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 => "[email protected]" 

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

你可以做一個單獨的控制器操作來通知引腳所有者並讓鏈接使用jquery來提交請求,然後不需要頁面加載,並且你可以通知它被髮送的通知也由jquery處理。

我不知道你的代碼庫在所有所以這個SA粗糙例子

# 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: 
    }) 
    }) 
}) 

希望幫助

相關問題