本文介绍了如何在Rails 3中使用Draper装饰嵌套属性(关联)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的环境:
- 导轨3.2
- 带有 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
我尝试用ItemDecorator
替换items
,但是没有用.我应该放在哪里?
我知道Draper在表单中的嵌套资源有问题,但这不是表单!
解决方案
据我对您的问题的正确理解,您有一个模型user
,其中有很多items
,但是您的items
却没有装饰?
因此添加到您的UserDecorator
:
class UserDecorator < Draper::Base
decorates :user
decorates_association :items #, :with => :item
[..]
end
class ItemDecorator < Draper::Base
decorates :item
[..]
end
请查看来源. /p>
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!
解决方案
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.
这篇关于如何在Rails 3中使用Draper装饰嵌套属性(关联)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!