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

时间:2012-06-04 16:30:39

标签: ruby-on-rails nested-attributes draper

我的环境:

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

#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替换为ItemDecorator,但它无效。我应该把它放在哪里?

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

1 个答案:

答案 0 :(得分:11)

据我所知,你的模型user有很多items但你的items没有被装饰?

添加到您的UserDecorator

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

  [..]
end

class ItemDecorator < Draper::Base
  decorates :item

  [..]
end

查看the source