【发布时间】:2013-02-08 20:32:57
【问题描述】:
突然在我的 Rails 应用程序中,我不断收到双重渲染错误 (Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at mos ....etc...),我终生无法弄清楚双重渲染在哪里。当用户输入的查询格式不正确时,就会出现这种情况。这是检查格式并显示错误的代码:
def if_user_formulated_request_properly
unless request.post?
flash[:error] = "This page can only be accessed through the search page. (POST request only)"
redirect_to(:action => "index") and return
end
if params[:query].blank?
flash[:error] = "Search criteria can not be blank"
redirect_to(:action => "index") and return
end
if !(params[:query] =~ /-/)
flash[:error] = %( Format of search criteria is wrong.<br /> Should be [IXLSpecClass value][year]-[Message ID] for example GP07-8)
redirect_to(:action => "index") and return
end
if !(QueryParser.expression.match(params[:query]))
flash[:error] = %( Format of search criteria is wrong.<br /> Should be [IXLSpecClass value][year]-[Message ID] for example GP07-8)
redirect_to(:action => "index") and return
end
有什么建议吗?
更新
请求的控制器操作代码:
def show
if_user_formulated_request_properly do
@input_messages = InputMessage.search_by(params[:query].strip) unless params[:query].blank?
end
respond_to do |format|
format.html #default rendering
end
end
【问题讨论】:
-
你总是重定向到同一个动作,你为什么重复这些行?
-
@apneadivin 因为每一行显示不同的错误信息
-
是的,但这并不妨碍您将
redirect_to放在操作的最后。 -
@apneadiving 否,因为它仅在出现错误时重定向。如果没有错误,什么都不会发生
-
redirect_to (:action => "index") and return if flash[:error]怎么样
标签: ruby-on-rails