gpt4 book ai didi

ruby-on-rails - rails : App fails but code runs fine in console

转载 作者:太空宇宙 更新时间:2023-11-03 18:24:41 24 4
gpt4 key购买 nike

Controller 中的相关代码如下:

    logger.info("Dumping params")
logger.info(params)
logger.info("Dumping initial cost:")
logger.info(@cost)
logger.info("entering if statement")
if params.has_key?("special_edition") && params["special_edition"] == 'yes'
@cost = @cost + 50000
end
logger.info("If we get to here then the first conditional executed correctly")
if params.has_key?("number_of_lids") && params["number_of_lids"].to_i > 0
@cost = @cost + (params["number_of_lids"].to_i * 5000)
end
logger.info("If we get to here then the second conditional executed correctly")
logger.info("printing final cost:")
logger.info(@cost)

当我运行该应用程序时,出现 500 错误。检查日志文件(测试日志文件),我看到以下内容:

Dumping params
{"utf8"=>"✓", "special_edition"=>"yes", "number_of_lids"=>"3", "action"=>"create"}
Dumping initial cost:
350000
entering if statement
Completed 500 Internal Server Error in 1922ms

如果我进入控制台(rails 控制台),并运行这段代码(从日志文件中获取的值:

params = {"utf8"=>"✓", "special_edition"=>"yes", "number_of_lids"=>"3", "action"=>"create"}
@cost = 350000
if params.has_key?("special_edition") && params["special_edition"] == 'yes'
@cost = @cost + 50000
end
if params.has_key?("number_of_lids") && params["number_of_lids"].to_i > 0
@cost = @cost + (params["number_of_lids"].to_i * 5000)
end

然后我得到@cost: 415000 的正确结果

我可能会收到 500 错误的任何想法?

澄清:

有几个回复提到不同之处在于我正在初始化 @cost 但不是在 Controller 中进行。不包括初始化@cost 的代码,因为它已经正确初始化。我包含了一段将@cost 记录到日志文件的代码,我正在控制台中使用我从日志文件中为@cost 获取的值对其进行初始化(请参阅我的代码,第 3 行和第 4 行,然后是日志文件第 3 和 4 行)

我尝试使用评论功能,但 stackoverlfow 给我一条错误消息。

决议

事实证明,应用程序中的另一个模块正在读取 interget @cost 并将其转换为字符串。这被应用程序中的另一个错误掩盖了,因此较早的测试失败了。这个故事的寓意:回归测试是必不可少的。使用@cost.to_i 解决了这个问题

最佳答案

显式@cost 初始化的区别。在您的 Controller 操作开始处写入 @cost = 350000

关于ruby-on-rails - rails : App fails but code runs fine in console,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13054957/

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