Jakov
Jakov

Reputation: 151

How to use decorator in rails correctly?

I'm newbie in using decorators and I need your help to understand how to use it right and what am I doing wrong.

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)
...

I've followed these tutorials (https://github.com/netguru/decent_decoration https://github.com/drapergem/draper#decorating-objects) and it seems to be fine, but when i'm on books index page, it says

undefined method `in_users_list?' for Book:0x007f6f4a0a4a18

I suppose that it still doesn't know that it should use method from decorator, but how to fix it? I can't understand what I've done wrong, please help to find and fix problem! Thank you in advance!

Upvotes: 1

Views: 1395

Answers (1)

Jakov
Jakov

Reputation: 151

Well, I just had to add expose_decorated(:books) in books_controller. I thing that it's because I'm using it in index method, so books (not just book) should be decorated

Upvotes: 1

Related Questions