我一直得到DoubleRenderError,我无法弄清楚为什么!基本上,我有一个动作,调用另一个动作,检查用户输入的查询错误,如果有错误,它停止并显示错误.但是当我输入一个带有错误的查询时,当我得到双重渲染时!有什么建议吗?
继承错误检查器操作:
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 exam ple 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 examp le GP07-8) redirect_to(:action => "index") and return end yield
以防您需要调用此操作的操作..
def show if_user_formulated_request_properly do @statuses = IXLStatus.find(:all) @input_messages = InputMessage.search_by(params[:query].stri p) unless params[:query].blank? @query = params[:query] end respond_to do |format| format.html #default rendering end end end
UPDATE
也忘了提,这最初是一个rails 2应用程序,并正在工作,这个错误开始时,我升级到rails 3(我相信),所以也许rails 3做了不同的东西并返回?
解决方法
您只是从if_user_formulated_request_properly方法返回,这意味着redirect_to和respond_to都进行渲染.
你可以试试这个:
def user_formulated_request_properly? unless request.post? flash[:error] = "This page can only be accessed through the search page. (POST request only)" return false end if params[:query].blank? flash[:error] = "Search criteria can not be blank" return false end if !(params[:query] =~ /-/) flash[:error] = "( Format of search criteria is wrong.<br /> Should be [IXLSpecClass value][year]-[Message ID] for example GP07-8)" return false 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) return false end return true end def show if user_formulated_request_properly? @statuses = IXLStatus.find(:all) @input_messages = InputMessage.search_by(params[:query].strip) unless params[:query].blank? @query = params[:query] else redirect_to(:action => "index") and return end respond_to do |format| format.html #default rendering end end