我有个模特叫“理发师”。
我像这样安装了Draper
gem 'draper', '~> 4.0', '>= 4.0.2'
then
bundle install
then
rails generate decorator Barber这是Decorator类
include Draper::LazyHelpers
class BarberDecorator < Draper::Decorator
delegate_all
def fullname
object.first_name + "Barber"
end
end我希望它在显示我的first_name文件时将"Barber“添加到barbers/index.html.erb列中
这是该视图的代码。
<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>我试图通过在这一行<td><%= barber.first_name.decorate%></td>上调用它来使用装饰器
但它会抛出这个错误。
NoMethodError in Barbers#index
undefined method `decorate' for "test2":String,我在这里做错什么了?
更新--我现在已经在我的controllers/barbers_controller.rb中尝试过了
def show
@barber = Barber.find(params[:id]).decorate
end当我去show.html.erb的时候
<th scope="row"><%= @barber.first_name.fullname %></th>我知道这个错误
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发布于 2022-03-30 12:33:41
经过在评论部分的讨论和更多的挖掘,下面是我为使它正确工作所做的工作。
#In my barbers controller
def index
@barbers = Barber.all.decorate
end在我看来
<td><%= barber.barber %></td>其中.barber是我的barber_decorator.rb文件中方法的名称。
https://stackoverflow.com/questions/71666157
复制相似问题