如何显示特定的帖子??Actioncontroller Ruby on Rails
我正在尝试制作播客页面。在索引页面上,我想在顶部显示最新的播客,在中间显示接下来的三个播客,在页面底部显示其余的播客 例如,我有25集,希望如下所示 25岁 22、23、24在中间 21,20,19,18~1在底部 我的控制器如何显示特定的帖子??Actioncontroller Ruby on Rails,ruby,ruby-on-rails-4,actioncontroller,Ruby,Ruby On Rails 4,Actioncontroller,我正在尝试制作播客页面。在索引页面上,我想在顶部显示最新的播客,在中间显示接下来的三个播客,在页面底部显示其余的播客 例如,我有25集,希望如下所示 25岁 22、23、24在中间 21,20,19,18~1在底部 我的控制器 class PodcastsController < ApplicationController before_action :find_podcast, only: [:show, :edit, :update, :destroy] # GET /pod
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
到目前为止,我可以显示最新的一个页面,但仍停留在按降序显示三个页面(第2、第3、第4)和其余页面(第5~all)
提前感谢您的帮助。首先,将您的限制更改为25
def index
@podcasts = Podcast
.order("created_at DESC")
.limit(25)
end
然后使用从阵列中取出所需的元素
latest = @podcasts.shift
next_three = @podcasts.shift(3)
the_rest = @podcasts
我想试试这样的。
首先,制作一个干净的控制器和一个查询对象来处理podcast列表逻辑会很好
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
class-PodcastsController
谢谢您的评论!。我尝试在模型和控制器中设置这三个,但都没有成功。当我将代码放入模型中时,我得到“undefined method error”,而控制器没有得到任何结果。如果你不介意的话,你能再详细一点吗?@carrey他们要么在视图中,要么在控制器中作为@latest=…
。对不起,但我在控制器中仍然得到一个命名错误#索引截图附在附件中。我的错误@播客不是数组,而是ActiveRecord::关系。最简单的方法是将其转换为一个数组,其中包含.to_a。这对于小的查询很好,但是对于大的查询可以避免。对不起,这很简单,我在打电话,没问题。有没有办法保持ActiveRecord:联系并实现我的目标?或者我应该把它改成数组?谢谢你的评论!。首先,我在model.rb文件中得到一个错误。第二,当我使用你的代码运行时,我得到了NoMEthodERror。我是否在视图文件中错误地使用了方法调用??是的,raise方法必须用()
包装,我将编辑我的回复。你必须记住,@最近的播客是一个对象,而不是数组,所以你不能使用。每个方法。您必须将视图分为三个部分,对于最近的,您应该使用@most\u recent\u podcast.eposion\u number
和。eposion\u title
。你不需要一个循环。但是有了@next|u three|u最近的
和@剩余的
是的,你必须做@next|u three。是的,就是这样。你知道我为什么会犯这个错误吗?将其更改为::PodcastQuery.new
谢谢!!你救了我一天。我可以成功地将所有这些应用到我的网站上,并且它可以无缝地工作。我很高兴。再次非常感谢你。!
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