如何显示特定范围内的帖子?? Rails 上的动作控制器 Ruby

How to display posts within specific range?? Actioncontroller Ruby on Rails

我正在尝试制作 Podcast 页面。在索引页面上,我想在顶部显示最新的播客在中间显示下三个播客其余全部在页面底部

比如我有25集,想像下面这样显示

25 at the top

22, 23, 24 in the middle

21,20,19,18 ~ 1 at the bottom

我的控制器

class PodcastsController < ApplicationController
  before_action :find_podcast, only: [:show, :edit, :update, :destroy]

  # GET /podcasts
  # GET /podcasts.json

  def index
    @podcasts = Podcast.order("created_at DESC").limit(1)

  end



  # GET /podcasts/1
  # GET /podcasts/1.json
  def show
    @podcasts = Podcast.all
  end

  # GET /podcasts/new
  def new
    @podcast = Podcast.new
  end

  # GET /podcasts/1/edit
  def edit
  end

  # POST /podcasts
  # POST /podcasts.json
  def create
    @podcast = Podcast.new(podcast_params)

    respond_to do |format|
      if @podcast.save
        format.html { redirect_to @podcast, notice: 'Podcast was successfully created.' }
        format.json { render :show, status: :created, location: @podcast }
      else
        format.html { render :new }
        format.json { render json: @podcast.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /podcasts/1
  # PATCH/PUT /podcasts/1.json
  def update
    respond_to do |format|
      if @podcast.update(podcast_params)
        format.html { redirect_to @podcast, notice: 'Podcast was successfully updated.' }
        format.json { render :show, status: :ok, location: @podcast }
      else
        format.html { render :edit }
        format.json { render json: @podcast.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /podcasts/1
  # DELETE /podcasts/1.json
  def destroy
    @podcast.destroy
    respond_to do |format|
      format.html { redirect_to podcasts_url, notice: "#{@pocast.title} was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def find_podcast
      @podcast = Podcast.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def podcast_params
      params.require(:podcast).permit(:episode_url, :episode_title, :episode_description, :episode_audio_url, :episode_number)
    end
end

index.html.haml

%section.no-spacing
  .row   
    .columns.large-12
      %h1
        %b
          Career Daily
        %p
          Need latest news on your career? Looking for advices? You found us! Andy Moore, the host of Career Dailey, delivers you the most...


    .columns.large-12
      - @podcasts.each do |podcast|
         %h3
           = podcast.episode_number
           = podcast.episode_audio_url
           = podcast.episode_description

我可以显示最新的一页,但卡在显示三页(第 2、3、4)和其余页面(第 5 ~ 所有)

在此先感谢您的帮助。

    @podcasts = Podcast.order("created_at DESC").limit(1)

您将结果限制为 1,只需删除 limit(1) 或使用分页将其设为 25。

我认为这是表示级逻辑,因此您可以提取最后的帖子:

def index
  @podcasts = Podcast.order("created_at DESC").limit(25)
end

然后在模板中

%section.no-spacing
  .row   
    - @podcasts.each_with_index do |podcast, index|
      - if index.zero?
        - # render first
      - elsif index < 4
        - # render 2, 3, 4
      - else
        - # render rest

您的@podcasts 对象包含您需要呈现的每个播客。

在您看来,- @podcasts.each do |podcast| 正在循环播放所有播客。不要全部循环,下面将为 2/3/4 创建循环,为其余创建一个单独的循环。

使用@podcasts[1..3].each do |podcast|循环第二、三、四。然后@podcasts[4..-1].each do |podcast|循环余数。

上面使用了ruby的数组索引方式;仅获取数组的一部分(所有播客)。查看 https://ruby-doc.org/core-2.7.1/Array.html#class-Array-label-Accessing+Elements 了解更多信息。

对于top podcast,不需要循环,直接渲染出来@podcasts.first.episode_description和其他你想渲染的属性。 Rails(可能是活动记录)只允许 .first 作为数组索引 [0]

的方便语法

您之前询问过控制器中的 3 个独立变量作为一种方法;你不希望这样——你最终会进行 3 个数据库查询而不是一个数据库查询——这真的很危险——要慢得多。