如何在 Rails 3 中使用 Draper 修饰嵌套属性(关联)?

2024-04-22

我的环境:

  • Rails 3.2
    • with draper https://github.com/jcasimir/draper gem

我正在使用嵌套资源,并且无法确定在哪里声明装饰器。

#app/controllers/users_controller.rb
def show
  @user = UserDecorator.find(params[:id])
  @items = @user.items
end

#app/controllers/items_controller.rb
def show
  @item = @user.items.find(params[:id])
end

我尝试更换items with ItemDecorator但它不起作用。我应该把它放在哪里?

我知道 Draper 在表单中嵌套资源有问题,但这不是表单!


据我正确理解你的问题,你有一个模型user其中有很多items,但是你的items没有装修吗?

所以添加到你的UserDecorator:

class UserDecorator < Draper::Base
  decorates :user
  decorates_association :items #, :with => :item 

  [..]
end

class ItemDecorator < Draper::Base
  decorates :item

  [..]
end

看看来源 https://github.com/jcasimir/draper/blob/master/lib/draper/base.rb.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Rails 3 中使用 Draper 修饰嵌套属性(关联)? 的相关文章

随机推荐