ruby-on-rails - 如何从 Ruby on Rails 的控制台调用 Controller / View 辅助方法?

标签 ruby-on-rails console

当我加载脚本/控制台时,有时我想使用 Controller 或 View 帮助器方法的输出。

有没有办法:

  • 模拟请求?
  • 根据所述请求从 Controller 实例调用方法?
  • 通过所述 Controller 实例或其他方式测试辅助方法?

最佳答案

要调用帮助程序,请使用 helper 对象:

$ ./script/console
>> helper.number_to_currency('123.45')
=> "R$ 123,45"

如果您想使用默认情况下未包含的帮助程序(例如,因为您从 ApplicationController 中删除了 helper :all),则只需包含该帮助程序即可。

>> include BogusHelper
>> helper.bogus
=> "bogus output"

至于处理 Controller ,我引用Nick's答案:

> 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 - 如何从 Ruby on Rails 的控制台调用 Controller / View 辅助方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/151030/

相关文章:

ruby-on-rails - 无法将 nil 转换为数组 will_paginate

python - 如何在不在控制台中重新键入的情况下逐步运行 python 脚本?

c++ - 将标准输出重定向回控制台

c - 知道如何在 C 中使用 TUI 制作按钮吗?

ruby-on-rails - 如何摆脱 Spring 警告 : Running `gem pristine --all` ?

ruby-on-rails - 编译器未能生成可执行文件。 (运行时错误)

ruby-on-rails - 带连接的 Rails named_scopes

ruby-on-rails - Rails : Display user name, 如果用户不存在,显示 'Anonymous' 相反

python - '\b' 不会在 PyCharm 控制台中打印退格

ios - 在ios模拟器中查看Console.log