2016-12-14 45 views
-1

我希望一個用戶能夠通過單擊按鈕向其他用戶發送電子郵件。他們會收到一封自動生成的電子郵件,說'你受到了[用戶]的挑戰!''因此,在視圖中會出現一個下拉菜單,從用戶列表中進行選擇,然後是一個「挑戰」按鈕,它會將此電子郵件發送給該用戶。我已經設置了發送註冊郵件的郵件程序。Ruby on Rails - 一位用戶發送電子郵件給另一位用戶 - 郵件

這裏的電子郵件:

<!DOCTYPE html> 
<html> 
<head> 
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type" /> 
</head> 
<body> 
<h1>You have been challeneged by, <%= @opponent.student_name %>.</h1> 

</body> 
</html> 

這裏是我的usermailer.rb(我的用戶被稱爲「學生」)

def welcome(student) 
    @student = student 

    mail(:to => student.email, :subject => "Welcome to the DIT Judo club!") 
    end 

    def challenge(student, opponent) 
    @student = student 
    @opponent = opponent 
    mail(:to => student.email, :subject => "You have been challeneged to a Judo match!") 
    end 
end 

下面是情況下,配置郵件的東西,你想看到它:

config.action_mailer.raise_delivery_errors = true 

    config.action_mailer.delivery_method = :smtp 
    config.action_mailer.smtp_settings ={ 
     :enable_starttls_auto => true, 
     :address => 'smtp.gmail.com', 
     :port => 587, 
     :domain => 'smtp.gmail.com', 
     :authentication => :plain, 
     :user_name => 'xxxxx', 
     :password => 'xxxxxx' 
    } 

所以基本上在一個視圖中,我想有一個下拉的地方,當前用戶(學生)選擇另一個用戶(學生),然後你可以點擊提交觸發挑戰郵件。我是新來的鐵軌有人可以幫我做嗎?

這裏是我的愚蠢的嘗試:

在路由文件夾:

get 'challenge', :to=>'usermailer#challenge' 

    resources :matches do 
     put :challenge 
    end 

在我的索引:

<div> 
Challenge another student to a match! 
    <div class="field"> 
    <%= f.label :opponent %><br> 

    <%= f.collection_select :opponent, Student.all, :id, :student_name %> 
    </div> 
    <%= link_to 'challenge', match challenge_path([current studentID ??], [opponent from collection]), method: :put %> 

</div> 

回答

0

welcomechallenge方法只是創建並返回一個郵件對象。您需要在郵件對象上致電deliver以實際發送郵件。有關更多詳細信息,請參見the documentation中的「致電郵件程序」。

+0

是的抱歉,我忘記補充說,在答案中,我有一條線,它爲註冊提供.deliver。我只是不知道如何爲我的挑戰課做到這一點!我不知道如何構建它,以便在視圖中可以挑選用戶進行挑戰,然後使其發生。 – NiallRedmond

+0

你會在任何控制器處理挑戰表單提交時執行此操作。 – Brian

+0

好吧,但我該怎麼做 – NiallRedmond

相關問題