How to decorate nested attributes (associations) with Draper in Rails 3?

孤街醉人 提交于 2020-01-01 04:27:29

问题


My environment:

  • Rails 3.2
    • with draper gem

I'm using nested resources and having trouble figuring out where to declare the decorator.

#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

I tried replacing items with ItemDecorator and it didn't work. Where should I be putting it?

I know that Draper has issues with nested resources in forms, but this isn't a form!


回答1:


As far as I've understood your problem correctly, you've a model user which has many items, but your items were not decorated?

So add to your UserDecorator:

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

  [..]
end

class ItemDecorator < Draper::Base
  decorates :item

  [..]
end

Have a look on the source.



来源:https://stackoverflow.com/questions/10884740/how-to-decorate-nested-attributes-associations-with-draper-in-rails-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!