为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕婉清6462132 2019-08-23 14:23:34
如何在Rails中从控制台调用控制器/视图方法?当我加载时script/console,有时我想要使用控制器的输出或视图助手方法。有办法:模拟请求?在所述请求上从控制器实例调用方法?通过所述控制器实例或其他方式测试助手方法?
查看完整描述

3 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

要调用帮助程序,请使用以下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


查看完整回答
反对 回复 2019-08-23
?
慕雪6442864

TA贡献1812条经验 获得超5个赞

从脚本/控制台调用控制器操作并查看/操作响应对象的简单方法是:


> 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

app对象是ActionController :: Integration :: Session的一个实例

这适用于我使用Rails 2.1和2.3,我没有尝试早期版本。


查看完整回答
反对 回复 2019-08-23
  • 3 回答
  • 0 关注
  • 709 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信