我目前正在构建一个 Rails 应用程序,想知道是否有一种方法可以简化联系表单流程。最初我打算为主页建立一个联系表,并为“成为合作伙伴”页面建立一个联系表。每个都有自己的 Controller 和 html 电子邮件。然而,这让我想知道是否有一种方法可以让只有一个联系表单和 Controller 根据提交的页面生成适当的 html 电子邮件?基本上我希望客户收到一封电子邮件,其中包含“来自主页的新消息”或“来自成为合作伙伴页面的新消息”。
我确定有办法,但我很难弄清楚这种关系。
谢谢
感谢@cristiano 的回复,非常感谢。我不认为你可以为我安排好吗?我现在只是在挣扎。以下是我的文件:
contacts_controller.rb
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
if @contact.save
flash[:success] = "Message Sent!"
redirect_to new_contact_path
else
flash[:danger] = "Error occurred"
redirect_to new_contact_path
end
end
private
def contact_params
params.require(:contact).permit(:name, :phone, :email, :comments)
end
end
联系人.rb
class Contact < ActiveRecord::Base
validates :name, presence: true
validates :email, presence: true
after_create :send_email
private
def send_email
ContactMailer.contact_email(self).deliver
end
end
contact_mailer.rb
class ContactMailer < ActionMailer::Base
default to: 'justin@socialplayground.com.au'
def contact_email(contact)
@contact = contact
mail(from: @contact.email, subject: 'Contact Form Message').deliver
end
end
contact_email.html.erb
<!DOCTYPE Html>
<html>
<head>
<title></title>
</head>
<body>
<p>New message from RuNpiXelruN's Contact Form!, from <%= "#{@contact.name}, #{@contact.email}" %></p>
</br>
<p><%= @contact.name %></p>
<p><%= @contact.phone %></p>
<p><%= @contact.comments %></p>
</body>
</html>
它会在联系邮件的主题中,id 希望它从主页或合作伙伴注册页面等说出来
对不起,再次感谢
贾斯汀
最佳答案
您可以创建部分 _form.html.erb
并在任何您想要的地方调用它。
在您的 Controller 中,您可以通过调用 request.original_url
检查请求来自何处。
因此你的home.html.erb
可能是
<HTML for home page>
<%= render 'form' %>
<more HTML for home page>
您的become_a_partner.html.erb
可能是
<HTML for partner page>
<%= render 'form' %>
<more HTML for partner page>
你的_form.html.erb
可能是
<%= form_for(@contact) do |f| %>
<%= f.text_field :nome %>
<%= f.text_field :email %>
<%= f.text_area :message %>
<% end %>
最后,您可以检查请求来自何处,以便您可以设置正确的消息。
关于ruby - 导轨 : Is there a way of having only one contact form for a site that generates different HTML emails depending on what page it came from?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34582344/