以下代碼示例是在Ruby 1.9.3p484上運行的Rails 3.2.16應用程序的一部分。
每當創建新位置或更新位置時,應按照after_filter
中的定義發送消息。使用mail_form傳遞郵件時的DoubleRenderError
class LocationController < InheritedResources::Base
respond_to :json
after_filter :notify_location_contact, only: [:create, :update]
def create
@location.user = current_user if current_user
create!
end
def update
update!
end
private
def notify_location_contact
message = MailForm.new
deliver_location_message(message)
end
def deliver_location_message(location_message)
begin
if location_message.deliver
render json: { message: "Successfully delivered" }, status: 201
else
render json: { error: "Delivery failure" }, status: 500
end
rescue => e
if e.is_a?(ArgumentError)
render json: { error: "Invalid Recipient" }, status: 422
else
render json: { error: e.message }, status: 500
end
end
end
end
該消息本身被髮送。雖然,deliver_location_message
首先呈現「成功傳遞」塊,並在最後一塊呈現錯誤消息之後呈現。這會導致內部服務器錯誤:
Completed 500 Internal Server Error
AbstractController::DoubleRenderError - Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
爲了發送使用mail_form gem~> 1.5.0
該消息。
DoubleRenderError似乎發生是因爲create
和update
都在他們完成工作時呈現JSON響應。之後,.deliver
呈現其JSON響應以通知成功或失敗。
我試過了。雖然同樣的錯誤。 – JJD
@JJD,你可以在你的問題中發表你的完整曲線嗎? – vee
這是[stacktrace](http://pastebin.com/a5mZxfme)。 – JJD