Rails 4 的动作邮件程序
Posted
技术标签:
【中文标题】Rails 4 的动作邮件程序【英文标题】:action mailer for rails 4 【发布时间】:2014-11-09 04:43:41 【问题描述】:我一直在关注一个相当古老的教程,以便创建一个联系表单,然后创建一个发送给站点管理员的邮件。我认为我遇到了一些问题,因为我在 Rails 4.1.1 上并且教程太旧了!我想知道是否有人可以给我一些指导 - 我正在学习轨道。我目前得到的错误是 MessagesController 中的 NoMethodError#create undefined method `subject' for # 主题message.subject
这是我的代码
消息控制器:
class MessagesController < ApplicationController
before_action :set_message, only: [:show, :edit, :update, :destroy]
# GET /messages
# GET /messages.json
def index
@messages = Message.all
end
# GET /messages/1
# GET /messages/1.json
def show
end
# GET /messages/new
def new
@message = Message.new
end
# GET /messages/1/edit
def edit
end
# POST /messages
# POST /messages.json
def create
@message = Message.new(message_params)
respond_to do |format|
if @message.save
ContactMailer.deliver_message(@message)
flash.now[:notice] = 'Thank you for your message!'
format.html redirect_to root_path
format.json render :show, status: :created, location: @message
else
format.html render :new
format.json render json: @message.errors, status: :unprocessable_entity
end
end
结束
# PATCH/PUT /messages/1 # PATCH/PUT /messages/1.json 定义更新 respond_to 做 |格式| 如果@message.update(message_params) format.html redirect_to @message, notice: '消息已成功更新。' format.json 渲染:显示,状态::好的,位置:@message 别的 format.html 渲染:编辑 format.json 渲染 json:@message.errors,状态::unprocessable_entity 结尾 结尾 结束
# 删除 /messages/1 # 删除 /messages/1.json 定义破坏 @message.destroy respond_to 做 |格式| format.html redirect_to messages_url, notice: '消息被成功销毁。' format.json 头 :no_content 结尾 结束
私人的 # 使用回调在动作之间共享通用设置或约束。 def set_message @message = Message.find(params[:id]) 结束
# Never trust parameters from the scary internet, only allow the white list through.
def message_params
params.require(:message).permit(:name, :email, :company, :phone, :subject, :body)
end
结束
开发.rb:
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings =
address: "smpt.gmail.com",
port: "587",
domain: "gmail.com",
authentication: "plain",
enable_starttls_auto: true,
user_name: 'your_email_username',
password: 'your_email_password'
CONTACT_RECIPIENT = 'yourname@yourdomain.com'
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
# Required for Devise gem
config.action_mailer.default_url_options = :host => 'localhost:3000'
end
class ContactMailer < ActionMailer::Base
default from: "from@example.com"
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.contact_mailer.message.subject
#
def message(message)
subject message.subject
body :message => message
recipients CONTACT_RECIPIENT
from message.email
sent_on Time.now
end
end
end
message.html.erb
Email from your web site
From: <%= @message.name %>
Company: <%= @message.company %>
Phone: <%= @message.phone %>
Message: <%= @message.body %>
消息/_form.html.erb
<%= semantic_form_for @message do |f| %>
<%= f.inputs do %>
<%= f.input :name %>
<%= f.input :company %>
<%= f.input :phone %>
<%= f.input :email %>
<%= f.input :subject %>
<%= f.input :body %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :input %>
<% end %>
<% end %>
db 架构文件的相关部分
create_table "messages", force: true do |t|
t.string "name"
t.string "company"
t.string "phone"
t.string "email"
t.string "subject"
t.text "body"
t.datetime "created_at"
t.datetime "updated_at"
end
【问题讨论】:
你把你的ContactMailer
类放在development.rb
里面了吗?也许看看ActionMailer basics 也会有所帮助。
主题是消息表中的一列吗?您可以仔细检查您的 db/schema.rb 以验证它是否列在消息表下。此外,贾斯汀所说..您的 ContactMailer 应该在 app/mailers 中,而不是在 development.rb 中
我相信主题栏在那里-我已经相应地编辑了问题!
当您说将联系人邮件程序放入 development.rb 文件时,您可以扩展一下。在我在这里问之前,我已经用尽了我能找到的所有文档,包括那些文档!
【参考方案1】:
def deliver_message(message)
subject message.subject
body :message => message
recipients CONTACT_RECIPIENT
from message.email
sent_on Time.now
end
应该是
def deliver_message(message)
@message = message
mail(subject: message.subject, body: message.body, recipients: CONTACT_RECIPIENT, from: message.email, sent_on: Time.now)
end
【讨论】:
SyntaxError (/Users/em/new/app/mailers/contact_mailer.rb:26: 语法错误,意外 ':',期望关键字_end 主题:message.subject ^ /Users/em/new/ app/mailers/contact_mailer.rb:27: 语法错误,意外':',期待keyword_end body: :message => message ^ /Users/em/new/app/mailers/contact_mailer.rb:28: 语法错误,意外' :',期待keyword_end 收件人:CONTACT_RECIPIENT ^ /Users/em/new/app/mailers/contact_mailer.rb:29:语法错误,意外':',期待keyword_end 来自:message.email ^ /Users/em/new/app/mailers/contact_mailer.rb:30:语法错误,意外':',期待keyword_end sent_on:Time.now ^ /Users/em/new/app/ mailers/contact_mailer.rb:34:语法错误,意外的keyword_end,期望输入结束): /Users/em/new/app/mailers/contact_mailer.rb:26:语法错误,意外':',期望关键字_结束主题:message.subject,^ /Users/em/new/app /mailers/contact_mailer.rb:27:语法错误,意外 tLABEL,期待 '=' 正文:message.body,^ /Users/em/new/app/mailers/contact_mailer.rb:28:语法错误,意外 tLABEL,期待'=' 收件人:CONTACT_RECIPIENT,^ /Users/em/new/app/mailers/contact_mailer.rb:28:动态常量分配 /Users/em/new/app/mailers/contact_mailer.rb:30:语法错误,意外 tLABEL , 期待 '=' sent_on: Time.now ^以上是关于Rails 4 的动作邮件程序的主要内容,如果未能解决你的问题,请参考以下文章
Rails 4动作邮件程序SMTP-AUTH错误缺少秘密短语?
image_url 在动作邮件程序中不起作用(Ruby on rails)
Rails 4:EOFError:仅在开发中的任何电子邮件后到达文件结尾
Rails 4.2 - Sidekiq 在开发中不发送电子邮件