Rails-使用Draper Gem未定义的方法
我有这个称为“理发师”的模型。
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 <%= barber.first_name.decorate%>
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在评论部分进行了讨论并进行了更多挖掘之后,这是我为使其正确工作所做的。
然后,在我的视图中
,.barber是我
barber_decorator.rb
文件中我方法的名称。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.