Rails:应用程序失败但代码在控制台中运行良好

时间:2023-01-16 11:47:01

The relevant piece of code in the controller is the following:

控制器中的相关代码如下:

    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)

When I run the app, I get a 500 error. Checking into the log file (test log file), I see the following:

当我运行应用程序时,我收到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

If I enter the console (rails console), and run this code (values taken from the log file:

如果我进入控制台(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

Then I get the correct result for @cost: 415000

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

Any ideas why I might be getting a 500 error?

有什么想法我可能会得到500错误?

Clarification:

Several responses mentioned that the difference is that the difference is that I'm initializing @cost but not doing it in the controller. The code that's initializing @cost is not included, because it is already initializing properly. I included the piece of code that logs @cost to the log file and I am initializing it in the console using the value I am getting for @cost from the log file (see my code, lines 3 & 4 and then the output from the log file lines 3 & 4)

几个回复提到不同之处在于差异在于我正在初始化@cost而不是在控制器中执行它。初始化@cost的代码不包括在内,因为它已经正确初始化。我包含了将@cost记录到日志文件中的代码段,我在控制台中使用从日志文件中获取@cost的值来初始化它(请参阅我的代码,第3行和第4行,然后输出来自日志文件行3和4)

I tried to use the comment feature, but stackoverlfow is giving me an error message.

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

Resolution

It turns out that another module in the app was reading the interget @cost and turning it into a string. This was masked by another bug in the app, so an earlier test failed. Moral of the story: regression testing is essential. Using @cost.to_i fixed the problem

事实证明,应用程序中的另一个模块正在读取interget @cost并将其转换为字符串。这被应用程序中的另一个错误所掩盖,因此早期的测试失败了。故事的道德:回归测试至关重要。使用@ cost.to_i修复了问题

2 个解决方案

#1


1  

Just try

if params.has_key?(:special_edition) && params[:special_edition] == "yes"

if the error persists, just check if the @cost is an integer by assigning a value or with the to_i method.

如果错误仍然存​​在,只需通过分配值或使用to_i方法检查@cost是否为整数。

#2


2  

The difference in explicit @cost initialization. Write @cost = 350000 in start of your controller action.

显式@cost初始化的区别。在控制器操作开始时写@cost = 350000。

#1


1  

Just try

if params.has_key?(:special_edition) && params[:special_edition] == "yes"

if the error persists, just check if the @cost is an integer by assigning a value or with the to_i method.

如果错误仍然存​​在,只需通过分配值或使用to_i方法检查@cost是否为整数。

#2


2  

The difference in explicit @cost initialization. Write @cost = 350000 in start of your controller action.

显式@cost初始化的区别。在控制器操作开始时写@cost = 350000。