Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4:获取属于另一个对象的对象的下一个/上一个记录_Ruby On Rails_Object_Ruby On Rails 4_Model View Controller_Activerecord - Fatal编程技术网

Ruby on rails Rails 4:获取属于另一个对象的对象的下一个/上一个记录

Ruby on rails Rails 4:获取属于另一个对象的对象的下一个/上一个记录,ruby-on-rails,object,ruby-on-rails-4,model-view-controller,activerecord,Ruby On Rails,Object,Ruby On Rails 4,Model View Controller,Activerecord,在我的Rails 4应用程序中,我有一个Post和一个Calendar模型:一个Calendar有许多Post和一个Post属于aCalendar 在/postshow.html.erb视图(位于/posts/:id)中,我希望使用“上一篇文章”按钮和“下一篇文章”按钮,允许用户在当前文章所属日历的文章之间来回导航 以下是我的路线: resources :calendars do resources :posts, shallow: true end end 我知道我会在我的帖子s

在我的Rails 4应用程序中,我有一个
Post
和一个
Calendar
模型:一个
Calendar
有许多
Post
和一个
Post
属于
a
Calendar

/post
show.html.erb
视图(位于
/posts/:id
)中,我希望使用“上一篇文章”按钮和“下一篇文章”按钮,允许用户在当前文章所属日历的文章之间来回导航

以下是我的路线:

resources :calendars do
    resources :posts, shallow: true
  end
end
我知道我会在我的
帖子
show.html.erb
视图中看到类似的内容:

<% if @calendar.posts.count > 1 %>
  <%= link_to "< Previous", @previous_post %> | Post Preview | <%= link_to "Next >", @next_post %>
<% else %>
  Post Preview
<% end %>
但是,我正在努力为
previous
next
方法找到正确的定义(您可以在上面的
PostsController
代码中看到)

这些方法必须允许我分别在
@calendar.posts中查找当前帖子之前或之后的帖子


我怎样才能做到这一点呢?

听起来你需要一些分页。或者gems应该能做到(我更喜欢kaminari)

听起来你需要一些分页。或者gems应该能做到(我更喜欢kaminari)

不是一种非常理想的解决方案,但在您的情况下,应该可以从
@posts
数组中为您提供
@post
上一个
下一个
帖子

添加
get\u next\u previous\u posts
helper方法,并在控制器的
show
方法中使用它:

  def show
    @calendar = Calendar.find_by_id(@post.calendar_id)
    @posts = @calendar.posts
    @previous_post, @next_post = get_next_and_previous_posts(@posts, @post)
  end

  private

  def get_next_and_previous_posts(posts, current_post)
    next_post = posts.detect { |p| p.id > current_post.id }
    prev_post = posts.reverse.detect { |p| p.id > current_post.id }
    [prev_post, next_post]
  end

这不是一种非常理想的解决方案,但在您的情况下,应该可以从
@posts
数组中为您提供
@post
上一篇
下一篇
文章

添加
get\u next\u previous\u posts
helper方法,并在控制器的
show
方法中使用它:

  def show
    @calendar = Calendar.find_by_id(@post.calendar_id)
    @posts = @calendar.posts
    @previous_post, @next_post = get_next_and_previous_posts(@posts, @post)
  end

  private

  def get_next_and_previous_posts(posts, current_post)
    next_post = posts.detect { |p| p.id > current_post.id }
    prev_post = posts.reverse.detect { |p| p.id > current_post.id }
    [prev_post, next_post]
  end

这就是我最后做的:

post.rb 这不是一个理想的解决方案,但它正在发挥作用

-----

更新

因为帖子必须按时间顺序显示,我不得不将上面的代码更改为:

#post.rb

    def next
      calendar.posts.where("time > ?", time).first
    end

    def previous
      calendar.posts.where("time < ?", time).last
    end
仍然没有按预期工作


-----这就是我最后做的:

post.rb 这不是一个理想的解决方案,但它正在发挥作用

-----

更新

因为帖子必须按时间顺序显示,我不得不将上面的代码更改为:

#post.rb

    def next
      calendar.posts.where("time > ?", time).first
    end

    def previous
      calendar.posts.where("time < ?", time).last
    end
仍然没有按预期工作


-----

使用与日期相关的
next
previous
命令的解决方案是好的,但并不完全有效,因为您还需要按时间顺序对结果排序。
where
将过滤您不想要的内容,但您需要确保其余内容符合您想要的顺序

所以你会有这样的想法:

def next
  calendar.posts.where("time > ?", time).order(:time).first
end

def previous
  calendar.posts.where("time < ?", time).order(time: :desc).first
end
def next
calendar.posts.where(“time>?”,time).order(:time).first
结束
def先前版本
calendar.posts.where(“time<?”,time).order(time::desc).first
结束
编辑:
我假设时间是约会时间。如果只是一个没有日期信息的时间,您将迫切需要将其更改为DateTime字段。

使用与日期相关的
next
previous
命令的解决方案是好的,但不能完全工作,因为您需要按时间顺序对结果进行排序。
where
将过滤您不想要的内容,但您需要确保其余内容符合您想要的顺序

所以你会有这样的想法:

def next
  calendar.posts.where("time > ?", time).order(:time).first
end

def previous
  calendar.posts.where("time < ?", time).order(time: :desc).first
end
def next
calendar.posts.where(“time>?”,time).order(:time).first
结束
def先前版本
calendar.posts.where(“time<?”,time).order(time::desc).first
结束
编辑:
我假设时间是约会时间。如果这是一个只包含日期信息的时间,您将迫切需要将其更改为DateTime字段。

谢谢您的回答。不要误会(这里是新手),但是否值得实现分页以每页显示一条记录?我本以为分页对过滤具有特定间隔的记录很有用,比如10乘10。是的,我想是的。您需要跟踪您正在使用的记录和下一个记录,这有点棘手,而且可能需要更多的代码和更多的时间来实现。Kaminari很容易插入,特别是因为AJAX很容易使用,而且你可以有一个简单的“下一页”按钮。好的,谢谢你的解释。我会继续挖掘,看看是否有一种方法可以满足我的需求。如果没有,我会选择你推荐的一种宝石;)再次非常感谢。在我们讨论这个问题时,我认为您不应该将多个实例变量从控制器传递到视图。这对我来说是一个顿悟:要回答这个问题,您需要使用实例变量的方法来获取其他值。因此,您不必使用
@calendar
@post
@previous\u post
@nex\u post
,而是使用
@post
@calendar
@post.previous
@post.next
。谢谢您的回答。不要误会(这里是新手),但是否值得实现分页以每页显示一条记录?我本以为分页对过滤具有特定间隔的记录很有用,比如10乘10。是的,我想是的。您需要跟踪您正在使用的记录和下一个记录,这有点棘手,而且可能需要更多的代码和更多的时间来实现。Kaminari很容易插入,特别是因为AJAX很容易使用,而且你可以有一个简单的“下一页”按钮。好的,谢谢你的解释。我会继续挖掘,看看是否有一种方法可以满足我的需求。如果没有,我会选择你推荐的一种宝石;)再次非常感谢。在我们讨论这个问题时,我认为
def next
  calendar.posts.where("time > ?", time).order(:time).first
end

def previous
  calendar.posts.where("time < ?", time).order(time: :desc).first
end