首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails:为博客选择前6篇文章,然后选择第7篇,依此类推

Rails:为博客选择前6篇文章,然后选择第7篇,依此类推
EN

Stack Overflow用户
提问于 2017-03-08 03:39:28
回答 2查看 60关注 0票数 0

我是rails的新手,我正在创建一个类似于Esquire网站的博客,在那里,他们在页面顶部将最新的六个帖子排成一排,然后再往下一点,他们会有一个很大的帖子,继续帖子数组,所以它实际上是第7个帖子,然后再往下一点,你会有下三个帖子,8-11,等等。

我很难让控制器成为一个数组,在这个数组中,我可以调用前六个项目,然后在主页上分别调用第七个项目。

帖子控制器:

代码语言:javascript
复制
class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @posts = Post.all.order('created_at DESC')
    @topsix = @posts.take(6)
  end

  # GET /posts/new
  def new
    @post = current_user.posts.build
    @categories = Category.all.map{|c| [ c.name, c.id ] }
  end

  # GET /posts/1/edit
  def edit
    @categories = Category.all.map{|c| [ c.name, c.id ] }
  end

  # POST /posts
  # POST /posts.json
  def create
   @post = current_user.posts.build(post_params)
   @post.category_id = params[:category_id]
     respond_to do |format|
       if @post.save
          format.html { redirect_to @post, notice: "post was successfully created." }
           format.json { render :show, status: :created, location: @post }
       else
           format.html { render :new }
           format.json { render json: @post.errors, status: :unprocessable_entity }
       end
    end
  end

  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { render :show, status: :ok, location: @post }
      else
        format.html { render :edit }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
    @post.category_id = params[:category_id]
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:title, :excerpt, :create_date, :author, :body, :category_id, :image)
    end
end

家庭控制器:

代码语言:javascript
复制
class HomeController < ApplicationController
  def index
    @posts = Post.all.order('created_at DESC')
    @topsix = @posts.take(6)
    @first3 = @posts.order('created_at DESC').take(3)
    @sidebar = @posts.take(3)
    @first4 = @posts.take(4)
    @first = @posts.take(1)
  end
end

我在主页上将其命名为:

代码语言:javascript
复制
<div class="row no-gutters">
  <% @topsix.select { |post| post > 6 } %>
    <div class="col-md-2"><%= image_tag post.image.url(:large), class: "img-responsive"%>
      <h6 class="small-title"><%= link_to post.title, post %></h6>
    </div>
  <% end %>
</div>
EN

回答 2

Stack Overflow用户

发布于 2017-03-08 04:32:59

我认为你需要的是这样的东西:

代码语言:javascript
复制
@post1to6 = Post.all.order('created_at DESC').first(6) #top 6

@post7 = Post.all.order('created_at DESC').first(7).last #7th

num_post=Post.all.order('created_at DESC').count
@postRemainder = Post.all.order('created_at DESC').last(num_post-7) #Remainder
票数 0
EN

Stack Overflow用户

发布于 2017-03-08 04:36:46

代码语言:javascript
复制
all_posts = Post.all.order('created_at DESC')
first_seven_posts = all_posts.take(7)
@first_six_posts = first_seven_posts.take(6)
@seventh_post = first_seven_posts.last
@other_posts = all_posts.where('id NOT IN (?)', first_seven_posts.map(&:id))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42656895

复制
相关文章

相似问题

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