我在试着制作播客的页面。在索引页上,我想显示--在顶部显示最新的播客,在中间显示接下来的三个播客,在页面底部显示其余的内容。
例如,我有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
endindex.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我可以显示最新的一页,但只能显示三页(第二页、第三页、第四页)和其余的页(第五页至第五页)。
感谢你提前提供帮助。
发布于 2020-06-15 16:38:26
@ podcast对象包含需要呈现的每个podcast。
在您看来,- @podcasts.each do |podcast|正在遍历所有的播客。不要全部循环,下面将为2/3/4创建循环,为其余部分创建一个单独的循环。
使用@podcasts[1..3].each do |podcast|循环通过第2、第3和第4段。然后@podcasts[4..-1].each do |podcast|循环遍历剩余部分。
上面的方法使用ruby的数组索引方法;只获取数组的一部分(所有的播客)。有关这方面的更多信息,请看一下https://ruby-doc.org/core-2.7.1/Array.html#class-Array-label-Accessing+Elements。
对于顶级播客,您不需要循环,只需呈现出@podcasts.first.episode_description和其他要呈现的属性即可。Rails (特别是活动记录)只允许.first作为数组索引[0]的一种方便的语法。
作为一种方法,您前面询问了控制器中的3个独立变量;您不希望这样做--这是一个真正的危险,您最终将完成3DB查询,而不仅仅是一个查询--速度要慢得多。
发布于 2020-06-14 02:07:39
@podcasts = Podcast.order("created_at DESC").limit(1)您将结果限制为1,只需删除limit(1)或使用分页使其为25。
发布于 2020-06-14 14:02:39
我认为这是表示级逻辑,因此您可以提取最后的帖子:
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 resthttps://stackoverflow.com/questions/62367331
复制相似问题