Rails-使用Draper Gem未定义的方法

发布于 2025-01-17 23:38:52 字数 2184 浏览 2 评论 0原文

我有这个称为“理发师”的模型。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

御弟哥哥 2025-01-24 23:38:52

在评论部分进行了讨论并进行了更多挖掘之后,这是我为使其正确工作所做的。

#In my barbers controller

def index
    @barbers = Barber.all.decorate
end

然后,在我的视图中

<td><%= barber.barber %></td>

,.barber是我barber_decorator.rb文件中我方法的名称。

After a discussion in the comment section and more digging , here is what I done to have it working correctly.

#In my barbers controller

def index
    @barbers = Barber.all.decorate
end

Then in my view

<td><%= barber.barber %></td>

Where .barber is the name of my method inside my barber_decorator.rb file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文