如何在Ruby on Rails的控制台中调用控制器/视图帮助器方法? [英] How can I call controller/view helper methods from the console in Ruby on Rails?
本文介绍了如何在Ruby on Rails的控制台中调用控制器/视图帮助器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我加载 script / console
时,有时我想玩控制器或视图助手方法的输出。
When I load script/console
, sometimes I want to play with the output of a controller or a view helper method.
有没有办法:
- 模拟请求?
- 调用方法是否在上述请求中指定了控制器实例?
- 测试助手方法,是通过所述控制器实例还是其他方式?
推荐答案
要调用助手,请使用 helper
对象:
To call helpers, use the helper
object:
$ ./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"
关于与控制器打交道,我引用尼克的答案 :
> 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
这篇关于如何在Ruby on Rails的控制台中调用控制器/视图帮助器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文