【问题标题】:activescaffold @records manipulation in a before filter前过滤器中的 activescaffold @records 操作
【发布时间】:2009-11-24 07:30:04
【问题描述】:

我想打电话

@records.collect{|r| r.set_some_virtual_attribute(@context)}

在渲染 activescaffold 索引视图之前,但如果我这样做:

controller FooController < ApplicationController
  before_filter :change_things, :only => :index
  active_scaffold :foos

  protected

  def change_things
     @records.collect{|r| r.set_some_virtual_attribute(@context)}
  end
end

我明白了:

 You have a nil object when you didn't expect it!
 You might have expected an instance of Array. 
 The error occurred while evaluating nil.collect

调用索引视图时。如果我将过滤器放在 ActiveScaffold 调用之后,也会发生同样的事情。我可以采用某种不同的方法,但最重要的是,我需要根据控制器的某些上下文在@records 中设置每个对象的虚拟属性,以便在最终表格中显示

谢谢

-C

【问题讨论】:

    标签: ruby-on-rails callback activescaffold


    【解决方案1】:

    您实际上并不需要在控制器中访问此集合。我想做的事情可以通过定义适当的辅助方法来解决。

    【讨论】:

      【解决方案2】:

      我没有对此进行测试,但我认为应该是 @foos.collect 而不是 @records.collect

      【讨论】:

        猜你喜欢
        • 2012-01-09
        • 1970-01-01
        • 1970-01-01
        • 2021-11-29
        • 2012-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-19
        相关资源
        最近更新 更多