Rails 部分显示最新 5 个 kase

发布于 2024-08-31 12:45:55 字数 1100 浏览 1 评论 0原文

我的应用程序设置有一些运行良好的不同部分。我在这里询问如何获得部分工作以显示 kase 模型中的最新条目,但现在我需要在部分中显示 kase 模型中的最新 5 个条目。

我已经复制了显示最近的一个部分,它在我需要的地方工作,但只显示最后一个条目,我需要更改什么才能显示最后 5 个条目?

_recent_kases.html.erb

<% if Kase.most_recentfive %>
<h4>The most recent case reference is <strong><%= Kase.most_recentfive.jobno %></strong></h4>
<% end %>

kase.rb

  def self.most_recentfive
    first(:order => 'id DESC')
  end

谢谢,

丹尼

编辑

  def self.most_recentfive
      all(:order => 'id DESC', :limit=>5)
    end

如果我添加上面的代码,我会收到以下错误消息:

NoMethodError in Dashboard#index

Showing app/views/kases/_recent_kases.html.erb where line #2 raised:

undefined method `jobno' for #<Array:0x105984c60>
Extracted source (around line #2):

1: <% if Kase.most_recentfive %>
2: <h4>The most recent case reference is <strong><%= Kase.most_recentfive.jobno %></strong></h4>
3: <% end %>

I have my application setup with a few different partials working well. I have asked here how to get a partial working to show the latest entry in the kase model, but now I need to show the latest 5 entries in the kase model in a partial.

I have duplicated the show most recent one partial and it's working where I need it to but only shows the last entry, what do I need to change to show the last 5?

_recent_kases.html.erb

<% if Kase.most_recentfive %>
<h4>The most recent case reference is <strong><%= Kase.most_recentfive.jobno %></strong></h4>
<% end %>

kase.rb

  def self.most_recentfive
    first(:order => 'id DESC')
  end

Thanks,

Danny

EDIT

  def self.most_recentfive
      all(:order => 'id DESC', :limit=>5)
    end

If I add the above code I get the following Error Message:

NoMethodError in Dashboard#index

Showing app/views/kases/_recent_kases.html.erb where line #2 raised:

undefined method `jobno' for #<Array:0x105984c60>
Extracted source (around line #2):

1: <% if Kase.most_recentfive %>
2: <h4>The most recent case reference is <strong><%= Kase.most_recentfive.jobno %></strong></h4>
3: <% end %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

君勿笑 2024-09-07 12:45:55
  def self.most_recentfive
    all(:order => 'id DESC', :limit=>5)
  end

编辑添加:

然后,在您的部分中,要显示结果,您需要

<% if Kase.most_recentfive %>
<h4>The most recent five case references are
   <% Kase.most_recentfive.each do |k|%>
       <strong><%= link_to k.jobno, k %></strong><br />
   <% end %>
</h4>
<% end %>
  def self.most_recentfive
    all(:order => 'id DESC', :limit=>5)
  end

EDITED TO ADD:

Then, in your partial, to display the results, you do

<% if Kase.most_recentfive %>
<h4>The most recent five case references are
   <% Kase.most_recentfive.each do |k|%>
       <strong><%= link_to k.jobno, k %></strong><br />
   <% end %>
</h4>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文