如何在Rails中从控制台调用控制器/视图方法? [英] How do I call controller/view methods from the console in Rails?
本文介绍了如何在Rails中从控制台调用控制器/视图方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我加载 script / console
时,有时候我想使用控制器的输出或者视图帮助方法。
When I load script/console
, some times I want play with the output of a controller or a view helper method.
有办法:
- 模拟请求吗?
- 通过所述控制器实例或另一种方式,在所述请求上
- 测试帮助方法的控制器实例?
推荐答案
要调用助手,请使用助手
... hmm ... helper。
To call helpers, use the helper
…hmm… helper.
$ ./script/console
>> helper.number_to_currency('123.45')
=> "R$ 123,45"
如果你想使用默认不包括的帮手,因为您从帮助程序中删除了
ApplicationController
中的 helper:all
),只需包含帮助程序。
If you want to use a helper that's not included by default (say, because you removed helper :all
from ApplicationController
), just include the helper.
>> include BogusHelper
>> helper.bogus
=> "bogus output"
对于处理控制器 href =http://stackoverflow.com/questions/151030/how-do-i-call-controller-view-methods-from-the-console-in-rails/1436342#1436342> Nick's 答案:
As for dealing with controllers, I quote Nick's answer:
> 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
这篇关于如何在Rails中从控制台调用控制器/视图方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文