【发布时间】:2016-06-01 23:45:29
【问题描述】:
我是使用装饰器的新手,我需要你的帮助来了解如何正确使用它以及我做错了什么。
book_decorator.rb
class BookDecorator < ApplicationDecorator
delegate_all
def in_users_list?(user)
if user.books.exists?
true
end
end
end
views/books/index.html.slim
- if book.in_users_list?(current_user)
- button_text = 'I've read this book'
... #depict some buttons and links
books_controller.rb
class BooksController < ApplicationController
expose(:book, attributes: :book_params, finder_parameter: :id)
expose_decorated(:book, decorator: BookDecorator)
...
我已经按照这些教程 (https://github.com/netguru/decent_decorationhttps://github.com/drapergem/draper#decorating-objects) 进行操作,看起来还不错,但是当我在书籍索引页面上时,它会显示
未定义的方法`in_users_list?'对于书:0x007f6f4a0a4a18
我想它仍然不知道应该使用装饰器的方法,但是如何解决呢?我无法理解我做错了什么,请帮助查找并解决问题! 先感谢您!
【问题讨论】:
标签: ruby-on-rails decorator draper