2017-09-16 56 views
0

我是Rails的相对首发者,并且很好奇,如果有一种方法可以将网站的“联系人”页面上的消息发送到取决于所选主题的特定电子邮件地址。我用简单的表格,生成一种形式,这就是我有“联系”页面上:如何根据主题获取发送到特定电子邮件地址的邮件

<%= f.input :subject, collection: [:Curriculum, :Jobs, :Other], prompt: 'Subject', required: true, error: 'You must select one' %> 

在我contact.rb文件,我定义我的头方法:

def headers 
    { 
     subject: "Contact Form", 
     to: "******.*******@gmail.com", 
     from: %("#{name}" <#{email}>) 
    } 
end 

这是我想要完成的:

1)用户选择'课程'。消息被发送到,例如'[email protected]​​chool.org'。
2)用户选择'其他'。消息被发送到,例如'[email protected]'。
3)用户选择'工作'。消息被发送到,例如'[email protected]'。

任何帮助将不胜感激。

回答

0

开始的两个模型有一个一对多的关联设置:

class Inquiry < ApplicationRecord 
    belongs_to :subject 
end 

class Subject < ApplicationRecord 
    has_many :inquiries 
end 

虽然你可以使用硬编码的哈希,YAML文件或使用数据库中的行一些其他的解决办法解耦应用程序逻辑从具体的实施细节。在数据库中存储查询记录还可以提高您对电子邮件失败的恢复能力。

并让设置了查询路由控制器和视图:

resources :inquiries, only: [:new, :create] 

class InquiriesController 
    def new 
    @inquiry = Inquiry.new 
    end 

    def create 
    @inquiry = Inquiry.new(inquiry_params) 

    if @inquiry.save 
     # This is just an example showing how you would get the data: 
     send_mail(
     to: @inquiry.subject.email 
     subject: @inquiry.subject.title 
     body: @inquiry.message 
    ) 
    else 
     render :new 
    end 
    end 

    def inquiry_params 
    params.require(:inquiry).permit(:email, :name ,:message, :subject_id) 
    end 
end 

<%= form_for(@inquiry) do |f| %> 
    # ... 
    <div class="field"> 
    <%= f.label :subject_id, 'Subject' %> 
    <%= f.collection_select(:subject_id, Subject.all, :id, :name, prompt: true) %> 
    </div> 
    <%= f.submit 'Send' %> 
<% end %> 
+0

谢谢,马克斯! – AshNaz87

相关问题