【发布时间】:2020-06-15 02:31:42
【问题描述】:
我正在尝试制作 Podcast 页面。在索引页面上,我想在顶部显示最新的播客,在中间显示接下来的三个播客,在页面底部显示其余所有播客
例如,我有 25 集,想显示如下
25 在顶部
中间是22、23、24
21,20,19,18 ~ 1 在底部
我的控制器
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
- @podcasts.each do |podcast|
%h3
= podcast.episode_number
= podcast.episode_audio_url
= podcast.episode_description
到目前为止,我可以显示最新的一页,但坚持按降序显示三页(第 2、第 3、第 4)和其余页面(第 5 ~ 全部)。
提前感谢您的帮助。
【问题讨论】:
标签: ruby ruby-on-rails-4 actioncontroller