如何在Rails中从控制台调用控制器/视图方法?

如何在Rails中从控制台调用控制器/视图方法?

当我加载时script/console,有时我想要使用控制器的输出或视图助手方法。

有办法:

  • 模拟请求?

  • 在所述请求上从控制器实例调用方法?

  • 通过所述控制器实例或其他方式测试助手方法?


慕婉清6462132
浏览 712回答 3
3回答

Qyouu

要调用帮助程序,请使用以下helper对象:$ ./script/console>> helper.number_to_currency('123.45')=> "R$ 123,45"如果您想使用默认情况下未包含的帮助程序(例如,因为您已helper :all从中删除ApplicationController),只需包含帮助程序即可。>> include BogusHelper>> helper.bogus=> "bogus output"至于处理控制器,我引用尼克的回答:> app.get '/posts/1'> response = app.response# you now have a rails response object much like the integration tests> response.body            # get you the HTML> response.cookies         # hash of the cookies# etc, etc

慕雪6442864

从脚本/控制台调用控制器操作并查看/操作响应对象的简单方法是:> app.get '/posts/1'> response = app.response# you now have a rails response object much like the integration tests> response.body            # get you the HTML> response.cookies         # hash of the cookies# etc, etcapp对象是ActionController :: Integration :: Session的一个实例这适用于我使用Rails 2.1和2.3,我没有尝试早期版本。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Ruby