加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 23:22:37 所属栏目:百科 来源:网络整理
导读:我一直得到DoubleRenderError,我无法弄清楚为什么!基本上,我有一个动作,调用另一个动作,检查用户输入的查询错误,如果有错误,它停止并显示错误.但是当我输入一个带有错误的查询时,当我得到双重渲染时!有什么建议吗? 继承错误检查器操作: def if_user_form
我一直得到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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读