I have this model called " Barber " .
I have installed Draper like so
gem 'draper', '~> 4.0', '>= 4.0.2'
then
bundle install
then
rails generate decorator Barber
This is the Decorator class
include Draper::LazyHelpers
class BarberDecorator < Draper::Decorator
delegate_all
def fullname
object.first_name + "Barber"
end
end
I want it to add "Barber" to the first_name column when I display it my barbers/index.html.erb
file
Here is the code for that view.
<tbody>
<% @barbers.each do |barber| %>
<tr>
<td><%= barber.first_name.decorate%></td>
<td><%= barber.last_name %></td>
<td><%= barber.age %></td>
<td><%= barber.email %></td>
<td><%= barber.user_id %></td>
<td><%= link_to 'Show', barber %></td>
<td><%= link_to 'Edit', edit_barber_path(barber) %></td>
<td><%= link_to 'Destroy', barber, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
I try to use the decorator by calling it on this line <td><%= barber.first_name.decorate%></td>
But it throws this error.
NoMethodError in Barbers#index
undefined method `decorate' for "test2":String
What am I doing wrong here?
UPDATE
I have now tried this in my controllers/barbers_controller.rb
def show
@barber = Barber.find(params[:id]).decorate
end
Then when I go to show.html.erb
<th scope="row"><%= @barber.first_name.fullname %></th>
I get this error
Could not infer a decorator for Barber.
Extracted source (around line #14):
12
13
14
15
16
17
# GET /barbers/1 or /barbers/1.json
def show
@barber = Barber.find(params[:id]).decorate
end
After a discussion in the comment section and more digging , here is what I done to have it working correctly.
Then in my view
Where .barber is the name of my method inside my
barber_decorator.rb
file.