- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 Sendgrid Parse API 和 Griddler gem 来接受传入的电子邮件。在大多数情况下,这工作正常;但是,如果您未使用状态代码 200 响应 Sendgrid,他们将假定该应用程序未正确接收 POST 请求并继续尝试进行 POST 3 天。我正在尝试使用状态代码进行响应,但遇到了问题。
在常规的 RESTful 路由中,您可以执行类似...
render :status => 200
但是,我认为这必须在 Controller 中完成才能识别渲染方法。 Griddler 建议您创建一个 EmailProcessor 模型并使用“处理”方法来处理电子邮件。
据我了解,您不能在模型中使用渲染方法。因此,我使用类方法创建了一个 EmailProcessorsController 类,如下所示。
class EmailProcessor < ActiveRecord::Base
include ApplicationHelper
def initialize(email)
@email = email
@to = email.to # this is an array
@from = email.from
end
def process
# do other stuff
EmailProcessorsController.render_ok
end
end
class EmailProcessorsController < ActionController::Base
def self.render_ok
render :status => 200
end
end
下面是我从我的应用程序中得到的错误。它不喜欢渲染方法:(
NoMethodError (undefined method `render' for EmailProcessorsController:Class):
app/controllers/email_processors_controller.rb:6:in `render_ok'
app/models/email_processor.rb:16:in `process'
我是一名新开发人员,这可能非常简单,但我被卡住了。非常感谢对问题和设计的任何想法和评论。谢谢!
更新!
根据@meagar 的建议,我将渲染调用移到了 Controller ,如下所示,但现在我得到了一个不同的错误,我不知道该怎么做。
class EmailProcessorsController < ApplicationController
def initialize(email)
@email = email
@to = email.to # this is an array
@from = email.from
end
def process
# do other stuff
render :status => 200
end
end
没有渲染调用,我不会收到错误。这是我在调用渲染时遇到的错误...
Module::DelegationError (ActionController::RackDelegation#status= delegated to @_response.status=, but @_response is nil: #<EmailProcessorsController:0x000001063b1558 @email=#<Griddler::Email:0x00000106378ac8 @params={"headers"=>"Received: by mx-007.sjc1.send
最佳答案
render
是实例方法,不是类方法。您需要实例化 Controller 的一个实例,但这无论如何都行不通。
尝试从您的模型渲染是一个严重的错误。该模型不知道涉及 HTTP 请求。您的 Controller 应该创建您的模型,对其调用任何操作,等待成功,然后您的 Controller 应该呈现响应。您尝试做的事情从根本上是错误的。
关于ruby-on-rails - Rails Controller 中未定义的方法呈现 - 尝试使用 200 状态代码响应 Sendgrid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26153126/
我想了解 Ruby 方法 methods() 是如何工作的。 我尝试使用“ruby 方法”在 Google 上搜索,但这不是我需要的。 我也看过 ruby-doc.org,但我没有找到这种方法。
Test 方法 对指定的字符串执行一个正则表达式搜索,并返回一个 Boolean 值指示是否找到匹配的模式。 object.Test(string) 参数 object 必选项。总是一个
Replace 方法 替换在正则表达式查找中找到的文本。 object.Replace(string1, string2) 参数 object 必选项。总是一个 RegExp 对象的名称。
Raise 方法 生成运行时错误 object.Raise(number, source, description, helpfile, helpcontext) 参数 object 应为
Execute 方法 对指定的字符串执行正则表达式搜索。 object.Execute(string) 参数 object 必选项。总是一个 RegExp 对象的名称。 string
Clear 方法 清除 Err 对象的所有属性设置。 object.Clear object 应为 Err 对象的名称。 说明 在错误处理后,使用 Clear 显式地清除 Err 对象。此
CopyFile 方法 将一个或多个文件从某位置复制到另一位置。 object.CopyFile source, destination[, overwrite] 参数 object 必选
Copy 方法 将指定的文件或文件夹从某位置复制到另一位置。 object.Copy destination[, overwrite] 参数 object 必选项。应为 File 或 F
Close 方法 关闭打开的 TextStream 文件。 object.Close object 应为 TextStream 对象的名称。 说明 下面例子举例说明如何使用 Close 方
BuildPath 方法 向现有路径后添加名称。 object.BuildPath(path, name) 参数 object 必选项。应为 FileSystemObject 对象的名称
GetFolder 方法 返回与指定的路径中某文件夹相应的 Folder 对象。 object.GetFolder(folderspec) 参数 object 必选项。应为 FileSy
GetFileName 方法 返回指定路径(不是指定驱动器路径部分)的最后一个文件或文件夹。 object.GetFileName(pathspec) 参数 object 必选项。应为
GetFile 方法 返回与指定路径中某文件相应的 File 对象。 object.GetFile(filespec) 参数 object 必选项。应为 FileSystemObject
GetExtensionName 方法 返回字符串,该字符串包含路径最后一个组成部分的扩展名。 object.GetExtensionName(path) 参数 object 必选项。应
GetDriveName 方法 返回包含指定路径中驱动器名的字符串。 object.GetDriveName(path) 参数 object 必选项。应为 FileSystemObjec
GetDrive 方法 返回与指定的路径中驱动器相对应的 Drive 对象。 object.GetDrive drivespec 参数 object 必选项。应为 FileSystemO
GetBaseName 方法 返回字符串,其中包含文件的基本名 (不带扩展名), 或者提供的路径说明中的文件夹。 object.GetBaseName(path) 参数 object 必
GetAbsolutePathName 方法 从提供的指定路径中返回完整且含义明确的路径。 object.GetAbsolutePathName(pathspec) 参数 object
FolderExists 方法 如果指定的文件夹存在,则返回 True;否则返回 False。 object.FolderExists(folderspec) 参数 object 必选项
FileExists 方法 如果指定的文件存在返回 True;否则返回 False。 object.FileExists(filespec) 参数 object 必选项。应为 FileS
我是一名优秀的程序员,十分优秀!