gpt4 book ai didi

ruby-on-rails - 为什么会出现这种双重渲染错误?

转载 作者:行者123 更新时间:2023-12-04 20:08:03 25 4
gpt4 key购买 nike

在我的 Rails 应用程序中,突然间我不断收到双重渲染错误(在此操作中多次调用渲染和/或重定向。请注意,您只能调用渲染或重定向,并且在 mos 。 ...等...),我一生都无法弄清楚双重渲染在哪里。当用户输入的查询格式不正确时,就会出现这种情况。这是检查格式并显示错误的代码:

 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

有什么建议吗?

更新

请求的 Controller 操作代码:

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

最佳答案

我会建议对操作代码进行重构,并留给您重构其余部分。 . .

def show
unless user_formulated_request_properly?
redirect_to(:action => "index")
return
end
respond_to do |format|
format.html
end
end
如果不明显,您不应该在 user_formulated_request_properly? 中进行任何重定向调用,也不应该调用 yield。 (恕我直言, yield 是最过度使用的 ruby​​ 语言功能)

关于ruby-on-rails - 为什么会出现这种双重渲染错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7196540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com