【问题标题】:How to use decorator in rails correctly?如何在 Rails 中正确使用装饰器?
【发布时间】: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


    【解决方案1】:

    好吧,我只需要在 books_controller 中添加expose_decorated(:books)。我认为这是因为我在索引方法中使用它,所以应该装饰书籍(不仅仅是书籍)

    【讨论】:

      猜你喜欢
      • 2011-12-15
      • 2014-10-17
      • 2019-11-17
      • 2021-01-17
      • 2021-05-30
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多