Rails 邮件表单 gem 不工作
Rails MailForm gem not working
我正在尝试使用 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
我已经尝试了几乎所有关于堆栈溢出的答案,但没有任何效果。我在这里做错了什么?
我想你忘了在 contact_params
中允许 to_email
def contact_params
params.require(:contact).permit(:from_email, :from_name, :subject, :message, :to_email)
end
我正在尝试使用 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
我已经尝试了几乎所有关于堆栈溢出的答案,但没有任何效果。我在这里做错了什么?
我想你忘了在 contact_params
to_email
def contact_params
params.require(:contact).permit(:from_email, :from_name, :subject, :message, :to_email)
end