Rails / Mailer - 发送电子邮件后的NoMethodError

时间:2023-01-16 10:19:02

I'm fairly new to Rails, so this will likely end up being something obvious; however I've just spent the better part of a day pulling my hair out over this issue.

我对Rails相当新,所以这可能最终成为明显的东西;然而,我只是花了一天的时间把头发拉出来解决这个问题。

I have a rails app which I've been working on for awhile, however I only started implementing mailers today. I followed the Rails ActionMailer tutorial here: http://guides.rubyonrails.org/v3.0.3/action_mailer_basics.html and the mailer works fine in a new example app. However, when I repeated those steps verbatim in my existing rails app (running in Development environment) I receive the below error. It creates the entry in the DB, correctly sends both the plain text & HTML emails and THEN generates the error. All I'm trying to do here is send a welcome email upon the creation of a new account, but I'm getting the same error when I try to send any email from any controller.

我有一个rails应用程序,我已经工作了一段时间,但我今天才开始实现邮件程序。我在这里遵循Rails ActionMailer教程:http://guides.rubyonrails.org/v3.0.3/action_mailer_basics.html并且邮件程序在新的示例应用程序中正常工作。但是,当我在现有的rails应用程序(在开发环境中运行)中逐字重复这些步骤时,我收到以下错误。它在数据库中创建条目,正确发送纯文本和HTML电子邮件,然后生成错误。我想在这里做的就是在创建新帐户时发送欢迎电子邮件,但是当我尝试从任何控制器发送任何电子邮件时,我收到同样的错误。

The specific error I'm seeing after it sends the welcome email is:

我发送欢迎电子邮件后发现的具体错误是:

    Completed 500 Internal Server Error in 280ms

NoMethodError (undefined method `error' for true:TrueClass):
  app/controllers/musers_controller.rb:52:in `block in create'
  app/controllers/musers_controller.rb:50:in `create' 

Note that to not mess up my existing User table, I created a temporary scaffold & mailer called Muser which I plan on deleting once I'm confident this will work correctly on my user table.

请注意,为了不弄乱我现有的User表,我创建了一个名为Muser的临时脚手架和邮件程序,我打算删除一次,我相信它可以在我的用户表上正常工作。

Code

Error in log:

Started POST "/musers" for 127.0.0.1 at 2013-07-10 20:32:34 -0400
Processing by MusersController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"OuoEmsjkAVBHZwqPO5b/O4eKw6iZBaLP6vUT6f9WCOI=", "muser"=>{"name"=>"New User", "email"=>"User@email.com"}, "commit"=>"Create"}
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
   (0.1ms)  begin transaction
  SQL (0.6ms)  INSERT INTO "musers" ("created_at", "email", "name", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", Thu, 11 Jul 2013 00:32:34 UTC +00:00], ["email", "User@email.com"], ["name", "New User"], ["updated_at", Thu, 11 Jul 2013 00:32:34 UTC +00:00]]
   (1.7ms)  commit transaction
  Rendered muser_mailer/registration_confirmation.html.erb (0.1ms)
  Rendered muser_mailer/registration_confirmation.text.erb (0.0ms)
Completed 500 Internal Server Error in 280ms

NoMethodError (undefined method `error' for true:TrueClass):
  app/controllers/musers_controller.rb:52:in `block in create'
  app/controllers/musers_controller.rb:50:in `create'


  Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_trace.erb (2.6ms)
  Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.5ms)
  Rendered /usr/local/rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.2.8/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (15.3ms)

--I should note that lines 50 & 52 of the musers_controller (where this error is being generated) correspond to the 'respond_to do' & 'MuserMailer.... .deliver' lines in the controller code below.--

- 我应该注意musers_controller的第50和52行(生成此错误)对应于下面控制器代码中的'respond_to do'和'MuserMailer .... .deliver'行.--

The controller action:

# POST /musers
  # POST /musers.json
  def create
    @muser = Muser.new(params[:muser])

respond_to do |format|
  if @muser.save
    MuserMailer.registration_confirmation(@muser).deliver
    format.html { redirect_to @muser, notice: 'Muser was successfully created.' }
    format.json { render json: @muser, status: :created, location: @muser }
  else
    format.html { render action: "new" }
    format.json { render json: @muser.errors, status: :unprocessable_entity }
  end
end
  end

Mailer:

class MuserMailer < ActionMailer::Base
   default from: "EmailAddress@Inter.net"

  def registration_confirmation(muser)
    @muser = muser
    mail(:to => muser.email, :subject => "Registered")
  end
end

I don't think that the issue is with my smtp, mail setup, or variables since it does actually add to the DB & send the emails correctly. If I comment out the line in the controller which calls the mail action the error disappears, so I don't think the problem is with my muser routes. This undefined method 'error' for true:TrueClass is driving me nuts. I did recently install Devise on my Users table, so I don't know if that could be causing the issue?

我认为问题不在于我的smtp,邮件设置或变量,因为它确实添加到数据库并正确发送电子邮件。如果我注释掉控制器中调用邮件操作的行,则错误消失,所以我认为问题不在于我的muser路由。这个未定义的方法'错误'为true:TrueClass让我疯了。我最近在我的用户表上安装了Devise,所以我不知道这是否会导致问题?

For lack of a better term, it feels like the issue is with how Rails wants to route after sending the emails; as if I need to put a Return or specify a route at the end of my mailer action telling the server to head back to the controller action. In other words, I'm lost!

由于缺乏一个更好的术语,感觉问题是Rails在发送电子邮件后想要如何路由;好像我需要在邮件程序结束时放置一个Return或指定一条路由,告诉服务器返回控制器操作。换句话说,我迷路了!

Update

Below are the two mailer view files I'm using.

以下是我正在使用的两个邮件程序视图文件。

registration_confirmation.html.erb

<h3><%= @muser.name %>! You sweet sweet fool!</h3>
<p>Thank you for registering!</p>

registration_confirmation.text.erb

Thank you for registering!

Update 2

Here's my model for Muser:

这是我的Muser模型:

class Muser < ActiveRecord::Base
  attr_accessible :email, :name

end

1 个解决方案

#1


3  

I solved this issue - there was the errant line config.action_mailer.logger = true in my config/environments/development.rb file that was causing the issues. Once removed everything worked perfectly.

我解决了这个问题 - 我的config / environments / development.rb文件中存在错误的行config.action_mailer.logger = true导致问题。一旦删除一切都完美。

#1


3  

I solved this issue - there was the errant line config.action_mailer.logger = true in my config/environments/development.rb file that was causing the issues. Once removed everything worked perfectly.

我解决了这个问题 - 我的config / environments / development.rb文件中存在错误的行config.action_mailer.logger = true导致问题。一旦删除一切都完美。