我正在尝试使用MailForm gem来设置一个简单的电子邮件表单,但它不起作用,甚至没有给我任何消息。
这是我的控制器方法
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(params[:contact])
@contact.request = request
if @contact.deliver
flash.now[:notice] = "Thanks! I'll contact you soon."
else
flash.now[:error] = 'Cannot send message.'
render :new
end
end
private
def contact_params
params.require(:contact).permit(:from_email, :from_name, :subject, :message)
end
end
然后是视图(实际上是profiles/show
页面而不是contact/new
页面。这是问题吗?
<%= form_for @contact do |f| %>
<div class="form-group">
<%= f.label "Your Email" %>
<%= f.text_field :from_email, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label "Subject" %>
<%= f.text_field :subject, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label "Message" %>
<%= f.text_area :message, class: "form-control" %>
</div>
<div class="form-group">
<%= f.hidden_field :to_email, value: @user.email %>
<%= f.submit "Contact", class: 'btn btn-primary form-control' %>
</div>
<% end %>
我也认为这可能是一个配置问题。这是我用于gmail
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'gmail.com',
user_name: '<email>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true }
这是模型
class Contact < MailForm::Base
# belongs_to :user
attribute :to_email
attribute :name, :validate => true
attribute :from_email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
attribute :subject
attribute :message
def headers
{
subject: subject,
to: to_email,
from: <email>
}
end
end
我已经尝试了几乎所有关于堆栈溢出的答案,但没有任何工作。我在这里做错了什么?
答案 0 :(得分:0)
我想您忘记在to_email
contact_params
def contact_params
params.require(:contact).permit(:from_email, :from_name, :subject, :message, :to_email)
end