【问题标题】:How to set @virtual_path true in refresh() in ActionView::Template?如何在 ActionView::Template 的 refresh() 中将 @virtual_path 设置为 true?
【发布时间】:2013-09-17 10:30:48
【问题描述】:

我们在rails 3.2 应用程序中使用render inline:

<%= render inline: @erb_code, locals: {f: f} %>

这里@erb_code返回一个ERB code的字符串用于渲染。此内联的问题在于 @virtual_path 在渲染中设置为 false,这会导致 refresh(view) 的 ActionView::Template 中的规范错误。这是错误:

 ActionView::Template::Error:
       A template needs to have a virtual path in order to be refreshed

这是刷新定义中导致错误的行:

raise "A template needs to have a virtual path in order to be refreshed" unless @virtual_path

refresh(view) 的完整定义可在http://api.rubyonrails.org/classes/ActionView/Template.html 获得。

如何在 rspec 中设置@virtual_path true 以避免出现spec错误?但是代码的执行似乎没有错误。尝试在规范中将 true 分配给 @virtual_path,但它不起作用。

【问题讨论】:

  • 您使用的是什么规格? rspec、rails 测试等?
  • rspec 2.0 是我们正在使用的。
  • 您能否添加失败的测试的完整代码以及其他可能相关的内容?
  • 您是自己调用 refresh(如果是,为什么?)还是由 rspec 内部调用(了解有关 spec 文件的更多详细信息确实会有所帮助)
  • 不,我们没有调用刷新。但不知何故,它显示在错误中。

标签: ruby-on-rails render actionview


【解决方案1】:

你可以尝试在你的规范中存根这个刷新调用

ActionView::Template.any_instance.stub(:refresh)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 2018-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多