2016-04-30 14 views
1

以前問過這個問題,但是對給出的答案感到困惑,我無法準確理解如何實現這一點。在Rails應用程序用戶上創建一個聯繫表格以接收查詢

前一題Contact Form for User Profile

我一直在掙扎的方式來完成這一功能的思考。

目前我有一個用戶的個人資料。還有一個通用的聯繫表單模型,它不需要任何表單,並且不會將任何內容保存到數據庫中。

我的目標是建立一個一般的聯繫表格,我可以在其中鏈接個人用戶配置文件中的聯繫人按鈕。提交時的聯繫表格將被髮送到profile屬性中指定的用戶電子郵件。因此,例如,該配置文件有一個字段t.string contactform_email

目前我的聯繫模式已設置,可以發送到單個電子郵件。主要是應用程序的所有者。

class ContactMailer < ApplicationMailer 

     default :to => "[email protected]" 

     def contact_me(msg) 
     @msg = msg 

     mail from: @msg.email, subject: @msg.subject, body: @msg.content 
    end 
    end 

我的目標是簡單地鏈接

default :to => "[email protected]" 

喜歡的東西

 default :to => "@profile.contactform_email" 

用戶提交的答案中的鏈接問題,但我似乎無法準確地實現這個。

任何幫助將是非常有益的。

回答

0

如果你想發送到不同的接收者,並且沒有默認的to地址,那就不要使用它。只要指定的to:選項在mail方法:

# Remove this line 
default :to => "[email protected]" 

# Update your `mail` method 
mail to: @msg.contactform_email, from: @msg.email, subject: @msg.subject, body: @msg.content 

確保您@msgcontactform_email場或傳遞to場的@profile實例您的郵件:

class ContactMailer < ApplicationMailer 

    # default :to => "[email protected]" 

    def contact_me(msg, profile) 
    @msg = msg 
    mail to: profile.contactform_email, from: @msg.email, subject: @msg.subject, body: @msg.content 
    end 
end 

ContactMailer.contact_me(@msg, @profile).deliver_now 
相關問題