繁体   English   中英

"装饰器断言现有属性是未定义的方法"

[英]Decorator asserts that an existing attribute is undefined method

我在 Ruby on Rails 中,我正在使用 Draper 装饰器来“装饰”控制器,以便重构其属性并最终在控制器中呈现 json。 这些属性会形成自己的hash,在下面的例子中,你可以在Postman require中获取“id”和“name”键值对。

但是,我只在最新的关键“雇主”上得到错误。 要么维护一个简单的“employers: nil”,要么保持现在的状态 - 将特定内容放在另一个装饰器中(decorates_association :employers...等),将触发错误消息: undefined method `employers' for #<Tags::Event:0x00007f909955f768> Did you mean? employer_settings undefined method `employers' for #<Tags::Event:0x00007f909955f768> Did you mean? employer_settings

即使我将键的名称从雇主更改为雇主设置,仍会出现undefined method `employer_settings'设置”。 这很不寻常,我确定employer_settings 存在并且我已经成功地需要它的内容。

这是第一个装饰器的代码

class Api::EventDecorator < Draper::Decorator
  delegate_all
  decorates_association :employers, with: Api::Events::EmployerDecorator


  def attributes
    super.merge(
      {
        id:        nil, 
        name:      nil, 
        employers:  nil
      })
    end
end

这是第二个装饰器的代码

class Api::Events::EmployerDecorator < Draper::Decorator
  delegate_all

  def attributes
    {
     id: nil,
     employer_id: nil,
    }
  end
end

这是模型事件添加的关联

has_many :employers, class_name: 'Tags::Events::Employers', dependent: :destroy 

这是模范雇主添加的协会

belongs_to :event, class_name: 'Events::Event' 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM