制作“下一个”链接,但数据库中可能不存在 (id+1) 记录

发布于 2024-11-10 15:02:58 字数 999 浏览 0 评论 0原文

所以我想制作一个页面,通过从数据库中随机选择(最初)来显示Phrase。在该页面上我想要一个 <%= link_to "next"%> 但我想知道是否有一种有效的方法来确保

当前存在下一条记录我只是使用

# @phrase is current phrase   
<%= link_to "next", phrase_path( Phrase.find( @phrase.id + 1 ) ) %>

是的,我知道我应该从控制器调用 @next,或者更好的是在模型中使用 next 方法来调用 @phrase.next,但这只是为了说明目的。

但这通常会出现 ActiveRecord::RecordNotFound 错误,因为某些短语已从数据库中删除(由于审核、错误等......)。我可以从中解救并循环它,直到它在控制器中工作,然后传递它或其他东西,但这似乎是一个糟糕的解决方案,并且不是特别“railsy”,

是否有一个方便的解决方案,任何人都发现

基于

< a href="http://steve.dynedge.co.uk/2010/01/13/random-previous-and-next-entries-from-active-record-models-using-offset/" rel="nofollow" >this 链接有点过时,使用了 Rails 2 中的 named_scope 。我首先使用新的 Rails 3 scope 样式重写了它,但随后只是将其更改为方法。刚用过

def next 
   Phrase.where("id > ?", self.id).order("id ASC").first
end

def previous
   Phrase.where("id < ?", self.id).order("id DESC").first
end

so i want to make a page the displays a Phrase by select (initially) at random from the database. on that page i want a <%= link_to "next"%> but i was wondering if there was an efficient way to ensure that the next record exists

currently I'm using just

# @phrase is current phrase   
<%= link_to "next", phrase_path( Phrase.find( @phrase.id + 1 ) ) %>

yes, i know i should call a @next from the controller, or better yet have a next method in the model to call @phrase.next, but this is for illustrative purposes.

but this often turns up an ActiveRecord::RecordNotFound error because some phrases have been deleted from the db (due to moderation, error, etc...). I could rescue from this and loop that till it works in the controller then pass it or something, but that seems like a bad solution, and not particularly 'railsy'

is there a convenient solution to this anyone has found

figured it out

based on this link which is a little outdated, uses named_scope from back in rails 2. I first rewrote it using the new rails 3 scope style, but then just changed it to a method. just used

def next 
   Phrase.where("id > ?", self.id).order("id ASC").first
end

def previous
   Phrase.where("id < ?", self.id).order("id DESC").first
end

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

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

发布评论

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

评论(3

烟若柳尘 2024-11-17 15:02:58

尝试在模型上创建下一个/上一个范围,如 http://steve.dynedge.co.uk/2010/01/13/random-previous-and-next-entries-from-active-record- models-using-offset/

这将允许您执行以下操作:

Phrase.next(5) or Phrase.next(@phrase.id)

Try creating a next/previous scope on your model, as suggested in http://steve.dynedge.co.uk/2010/01/13/random-previous-and-next-entries-from-active-record-models-using-offset/

This will allow you to do something like:

Phrase.next(5) or Phrase.next(@phrase.id)
独闯女儿国 2024-11-17 15:02:58

为什么不在控制器中创建一个名为 next 的方法并传入当前记录 id。从那里将用户重定向回下一个资源的显示页面将是微不足道的。

如果您对提前创建链接犹豫不决,请考虑创建一个辅助方法来查找存在的下一条记录并使其在您的视图中可用。然后,每当您需要下一个可用记录的 ID 时,您就可以调用它。

Why don't you create a method in the controller called next and pass in the current record id. It would be trivial from there to redirect the user back to the show page for that next resource.

If you are deadset on creating the link in advance, look into creating a helper method to find the next record that exists and make it available in your views. Then you could call that whenever you needed the id of the next available record.

情徒 2024-11-17 15:02:58

will_paginate 之类的东西也可能有帮助。我知道你的页面大小只是一个,但你所做的本质是分页。

Something like will_paginate might be of help too. I know your page size is just one, but the essence of what you're doing is pagination.

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