I'm pretty new to rails and have been following a few tutorials for sending email with Action Mailer. The application I am working on consists of adding repair tickets and sending an email notification to the customer when a button is clicked. I have been able to send emails when creating a new repair ticket but have run into issues when trying to send the email via a button. I would greatly appreciate any suggestions regarding this issue.
我对rails非常陌生,并且已经关注了一些使用Action Mailer发送电子邮件的教程。我正在处理的应用程序包括添加修复票据并在单击按钮时向客户发送电子邮件通知。我在创建新维修票时能够发送电子邮件,但在尝试通过按钮发送电子邮件时遇到了问题。我非常感谢有关这个问题的任何建议。
Rails 4.2.0 Ruby 2.1.5 Ubuntu 14.0.4 PostgreSQL
Rails 4.2.0 Ruby 2.1.5 Ubuntu 14.0.4 PostgreSQL
I confirmed that my mail server settings were correct by calling my mail function from the create method in repair_tickets.rb when a new repair ticket is saved. I would like this same email notification sent when I click a button.
我确认我的邮件服务器设置是正确的,通过在保存新修复票证时从repair_tickets.rb中的create方法调用我的邮件功能。我想点击按钮时发送同样的电子邮件通知。
def create
@repair_ticket = RepairTicket.new(repair_ticket_params)
respond_to do |format|
if @repair_ticket.save
ExampleMailer.sample_email(@repair_ticket.customer.email).deliver_now
format.html { redirect_to @repair_ticket, notice: 'Repair ticket was successfully created.' }
format.json { render :show, status: :created, location: @repair_ticket }
else
format.html { render :new }
format.json { render json: @repair_ticket.errors, status: :unprocessable_entity }
end
end
end
Currently I can successfully view the HTML and plain-text emails in the browser via the preview page. (rails/mailers/example_mailer/sample_mail_preview) However, when I click my "Email Customer" button I receive the following error message.
目前,我可以通过预览页面在浏览器中成功查看HTML和纯文本电子邮件。 (rails / mailers / example_mailer / sample_mail_preview)但是,当我单击“电子邮件客户”按钮时,我收到以下错误消息。
NoMethodError in RepairTicketsController#send_order_mail
undefined method `customer' for nil:NilClass
def send_order_mail
ExampleMailer.sample_email(@repair_ticket.customer.email).deliver_now
end
# GET /repair_tickets/1
Rails.root: /home/vmann/newiab/iab-v2
Application Trace | Framework Trace | Full Trace
app/controllers/repair_tickets_controller.rb:13:in `send_order_mail'
Email Model mailers/example_mailer.rb
电子邮件模型邮件程序/ example_mailer.rb
class ExampleMailer < ActionMailer::Base
default from: "default@gmail.com"
def sample_email(customer)
@customer = customer
mail(to: @customer, subject: 'Sample Email')
end
end
repair_tickets_controller.rb
def send_order_mail
ExampleMailer.sample_email(@repair_ticket.customer.email).deliver_now
end
routes.rb
get :send_order_mail, to: 'repair_tickets#send_order_mail', as: :send_order_mail
repair_tickets\show.html.erb (where I have my button located)
repair_tickets \ show.html.erb(我的按钮所在的位置)
<%= link_to 'Email Customer', send_order_mail_path , class: "btn btn-primary" %>
1 个解决方案
#1
0
repair_tickets_controller.rb
def send_order_mail
ExampleMailer.sample_email(@repair_ticket.customer.email).deliver_now
end
The error message is saying @repair_ticket == nil, essentially you have not yet assigned a value to it, which needs to happen on the first line of the send_order_mail method. To do this, you will need to send an id with the url generated and sent by your button, which is then accessed by the controller to create the repair_ticket. For example, @repair_ticket = RepairTicket.find(params[:id]).
错误消息是说@repair_ticket == nil,基本上你还没有为它分配一个值,这需要发生在send_order_mail方法的第一行。为此,您需要发送一个id,其中包含由您的按钮生成并发送的url,然后由控制器访问该id以创建repair_ticket。例如,@ repair_ticket = RepairTicket.find(params [:id])。
#1
0
repair_tickets_controller.rb
def send_order_mail
ExampleMailer.sample_email(@repair_ticket.customer.email).deliver_now
end
The error message is saying @repair_ticket == nil, essentially you have not yet assigned a value to it, which needs to happen on the first line of the send_order_mail method. To do this, you will need to send an id with the url generated and sent by your button, which is then accessed by the controller to create the repair_ticket. For example, @repair_ticket = RepairTicket.find(params[:id]).
错误消息是说@repair_ticket == nil,基本上你还没有为它分配一个值,这需要发生在send_order_mail方法的第一行。为此,您需要发送一个id,其中包含由您的按钮生成并发送的url,然后由控制器访问该id以创建repair_ticket。例如,@ repair_ticket = RepairTicket.find(params [:id])。