代码之家  ›  专栏  ›  技术社区  ›  Steve

Draper gem未装饰关联模型

  •  0
  • Steve  · 技术社区  · 7 年前

    我正在遵循Draper gem文档,但相关模型没有被装饰。下面是我的顶级装饰师的一个片段:

    module Admin
      class OrderDecorator < Draper::Decorator
        delegate_all
        decorates_finders
        decorates_association :line_item
    
        def amount
          model.amount.nil? ? 0 : model.amount
        end
      end
    end
    

    下面是我的协会装饰师的一个片段:

    module Admin
      class LineItemDecorator < Draper::Decorator
        delegate_all
    
        def options
          model.options.present? ? model.options.to_s : 'N/A'
        end
      end
    end
    

    以下是控制器中调用装饰器的方法:

      def show
        @subject = Admin::OrderDecorator.find(params[:id])
      end
    

    它在视图中的使用方式如下:

    @subject.line_items.each do |li|
      = li.options
    

    ...等

    LineItemDecorator上的options方法被忽略了——如果它为nil,则不显示任何内容,而不是“不适用”。我把装订好了。撬入options方法,它就永远不会被调用。有人能看到我错过了什么吗?

    提前非常感谢。

    1 回复  |  直到 7 年前
        1
  •  1
  •   a131    7 年前

    我认为你需要用复数 line_items 非单数 line_item 使其发挥作用,因为这就是你的协会的名称。

     decorates_association :line_items