ruby-on-rails – 在此操作中多次调用渲染和/或重定向..?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 在此操作中多次调用渲染和/或重定向..?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直得到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
原文链接:https://www.f2er.com/ruby/264716.html

猜你在找的Ruby相关文章