首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何显示特定的帖子??动作控制器Rails

如何显示特定的帖子??动作控制器Rails
EN

Stack Overflow用户
提问于 2020-06-15 02:31:42
回答 2查看 75关注 0票数 0

我在试着制作播客的页面。在索引页上,我想显示--在顶部显示最新的播客,在中间显示接下来的三个播客,在页面底部显示其余的内容。

例如,我有25集,希望显示如下所示

25位于顶部

22,23,24在中间

底部21,20,19,18 ~1

我的控制器

代码语言:javascript
复制
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

代码语言:javascript
复制
%section.no-spacing
  .row   
    .columns.large-12
      - @podcasts.each do |podcast|
         %h3
           = podcast.episode_number
           = podcast.episode_audio_url
           = podcast.episode_description

到目前为止,我可以显示最新的一个,但停留在显示三个(第二,第三,第四)和其余的页(第五~所有)下降的顺序。

感谢你提前提供帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-15 05:06:58

我会尝试这样的方法。

首先,最好让一个干净的控制器和一个查询对象使用podcast列表逻辑。

代码语言:javascript
复制
class PodcastsController < ApplicationController

  def index
    podcast_query = PodcastQuery.new
    # this is one podcast object
    @most_recent_podcast = podcast_query.most_recent
    # these are arrays of podcasts
    @next_three_most_recent_podcasts = podcast_query.next_three_most_recent
    @remaining_podcasts = podcast_query.remaining
  end
end

# app/models/podcast_query.rb
class PodcastQuery
  def initialize
    @podcasts = Podcast.order("created_at DESC")
  end

  def most_recent
    @podcasts[0] || (raise StandardError.new "you need at least one Podcast")
  end

  def next_three_most_recent
    @podcasts[1..3] || []
  end

  def remaining
    @podcasts[4..-1] || []
  end
end
票数 1
EN

Stack Overflow用户

发布于 2020-06-15 02:48:10

首先,把你的限制改为25。

代码语言:javascript
复制
def index
  @podcasts = Podcast
    .order("created_at DESC")
    .limit(25)
end

然后使用Array#shift将需要的元素从数组中删除。

代码语言:javascript
复制
latest = @podcasts.shift
next_three = @podcasts.shift(3)
the_rest = @podcasts
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62380458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档