我已经尝试了我能想到的所有可能性,但只是在通过表单将数据发送到rails邮件时遇到同样的错误。知道为什么我在底部得到“没有找到MailerController#sign_up的模板”响应吗?
的routes.rb
scope '/mailer' do
post '/signup' => 'mailer#sign_up'
end
应用程序/控制器/ mailer_controller.rb
class MailerController < ApplicationController
def sign_up
#Create user object
@user = { first_name: params[:first_name],
last_name: params[:last_name],
email: params[:email],
package: params[:package_type],
username: params[:username],
followers: params[:followers],
age: params[:age],
hashtags: params[:hashtags],
comments: params[:comments]
}
AppMailer.sign_up(@user).deliver_now
end
end
应用程序/邮寄者/ app_mailer.rb
class AppMailer < ActionMailer::Base
default from: 'redacted_email'
def sign_up(user)
@user = user
mail(to: "redacted_email", from: @user['email'], subject: 'Sign Up | IAS')
end
end
应用程序/视图/ app_mailer / sign_up.html.erb
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<strong>First Name: </strong><p><%= @user[:first_name] %></p><br>
<strong>Last Name: </strong><p><%= @user[:last_name] %></p><br>
<strong>Email: </strong><p><%= @user[:email] %></p><br>
<strong>Package: </strong><p><%= @user[:package] %></p><br>
<strong>Username: </strong><p><%= @user[:username] %></p><br>
<strong>Current Followers: </strong><p><%= @user[:followers] %></p><br>
<strong>Age: </strong><p><%= @user[:age] %></p><br><br>
<strong>Hashtags: </strong><p><%= @user[:hashtags] %></p><br><br>
<strong>Comments: </strong><p><%= @user[:comments] %></p>
</body>
</html>
服务器响应
Started POST "/mailer/signup" for ::1 at 2017-10-23 15:15:48 -0400
Processing by MailerController#sign_up as HTML
Parameters: {redacted but all going through successfully}
Rendering app_mailer/sign_up.html.erb
Rendered app_mailer/sign_up.html.erb (1.7ms)
Rendering app_mailer/sign_up.text.erb
Rendered app_mailer/sign_up.text.erb (0.6ms)
AppMailer#sign_up: processed outbound mail in 395.7ms
Sent mail to redacted@email.com (25.0ms)
Date: Mon, 23 Oct 2017 15:15:49 -0400
To: redacted@email.com
Message-ID: <59ee3fe573c8_17c8e3fcc17436908788c@Sephs-MBP-5.fios-router.home.mail>
Subject: Sign Up | IAS
Mime-Version: 1.0
Content-Type: multipart/alternative;
boundary="--==_mimepart_59ee3fe529f6_17c8e3fcc1743690877b7";
charset=UTF-8
Content-Transfer-Encoding: 7bit
----==_mimepart_59ee3fe529f6_17c8e3fcc1743690877b7
Content-Type: text/plain;
charset=UTF-8
Content-Transfer-Encoding: 7bit
Mail unable to send. Please contact IAS Support.
----==_mimepart_59ee3fe529f6_17c8e3fcc1743690877b7
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: 7bit
<!DOCTYPE html>
<html>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<strong>First Name: </strong><p>redacted</p><br>
<strong>Last Name: </strong><p>redacted</p><br>
<strong>Email: </strong><p>redacted</p><br>
<strong>Package: </strong><p>$25/mo | Basic</p><br>
<strong>Username: </strong><p>redacted</p><br>
<strong>Current Followers: </strong><p>58.5k</p><br>
<strong>Age: </strong><p>1</p><br><br>
<strong>Hashtags: </strong><p>hash tagsss</p><br><br>
<strong>Comments: </strong><p>commentssss</p>
</body>
</html>
----==_mimepart_59ee3fe529f6_17c8e3fcc1743690877b7--
No template found for MailerController#sign_up, rendering head :no_content
Completed 204 No Content in 484ms
答案 0 :(得分:1)
您的问题是您错过了控制器的视图而不是邮件程序。邮件工作得非常好。问题是您通过HTTP POST将邮件发送到控制器。发送请求后,它不确定需要响应什么。
这是对控制器的JS / JSON请求吗?添加如下内容:
render json: { status: 'ok' }
到你的行动结束时应解决问题。