【问题标题】:How to display specific posts?? Actioncontroller Ruby on Rails如何显示特定帖子?动作控制器 Ruby on Rails
【发布时间】: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


    【解决方案1】:

    我会尝试这样的。
    首先,让一个干净的控制器和一个查询对象与播客列表逻辑一起工作会很棒。

    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
    

    【讨论】:

    • 感谢您的评论!首先,我在model.rb 文件prnt.sc/szudcz 中遇到错误。其次,当我使用你的代码运行时,我得到了 NoMETHodERror。我在视图文件中错误地使用了方法调用吗? prnt.sc/szuec0
    • 是的,raise 方法必须用( ) 包裹,我会编辑我的回复。您必须记住 @most_recent_podcast 是一个对象,而不是数组,因此您不能使用 .each 方法。您必须将视图分成三部分,对于最近的部分,您应该使用@most_recent_podcast.episode_number.episode_title。你不需要一个循环。但是@next_three_most_recent@remaining 是的,你必须在那里做@next_three.each do |podcast| 然后调用podcast.episode_number 等。
    • 好。是的,就是这样。你知道我为什么会收到这个错误吗? prnt.sc/szumvh
    • 改成::PodcastQuery.new
    • 谢谢!!你拯救了我的一天。我可以成功地将它们全部应用到我的网站上,并且可以无缝运行。我太高兴了。再次感谢您!
    【解决方案2】:

    首先,将限制更改为 25。

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

    然后使用Array#shift 将您需要的元素从数组中取出。

    latest = @podcasts.shift
    next_three = @podcasts.shift(3)
    the_rest = @podcasts
    

    【讨论】:

    • 感谢您的评论!我尝试在模型和控制器中设置这三个,但没有成功。当我将代码放入模型时,我得到“未定义的方法错误”,并且控制器没有得到任何结果。如果你不介意,你能再详细一点吗?
    • @carrey 他们要么在视图中,要么在控制器中作为@latest = ...
    • 抱歉,但我仍然在 controller#index prnt.sc/szsw3c 中收到 NoMethodError 屏幕截图。
    • 我的错误。 @podcasts 不是一个数组,而是一个 ActiveRecord::Relation。最简单的就是用 .to_a 把它变成一个数组。这对于小型查询很好,但对于大型查询要避免。抱歉,这很简短,我正在打电话。
    • 没问题。有没有办法保持 ActiveRecord:Relation 并实现我的目标?还是我应该将其更改为数组?
    猜你喜欢
    • 2012-04-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多