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

Ruby on rails Rails:为博客选择前6篇文章,然后选择第7篇,依此类推,ruby-on-rails,arrays,ruby,controller,Ruby On Rails,Arrays,Ruby,Controller,我是rails的新手,我正在创建一个博客,向一个Esquire网站寻找simliar,他们在页面顶部将最新的六篇帖子排成一行,然后再向下一点,他们将有一篇大帖子,它将延续帖子数组,所以这将是第七篇帖子,然后再向下一点将有接下来的三个职位8-11等 我很难让控制器成为一个数组,在这个数组中我可以分别调用前六项和第七项 主计长员额: class PostsController < ApplicationController before_action :set_post, only: [:

我是rails的新手,我正在创建一个博客,向一个Esquire网站寻找simliar,他们在页面顶部将最新的六篇帖子排成一行,然后再向下一点,他们将有一篇大帖子,它将延续帖子数组,所以这将是第七篇帖子,然后再向下一点将有接下来的三个职位8-11等

我很难让控制器成为一个数组,在这个数组中我可以分别调用前六项和第七项

主计长员额:

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
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
class PostsController
家庭控制器:

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
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
class HomeController
我在主页上称之为:

<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>

6 } %>

我想你需要的是这样的东西:

@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

你需要的是:1)前6个帖子,2)第7个帖子,和3)其余的帖子?是的,准确地说,并显示在主页上page@Ian,请测试我的答案。很好的解决方案,但可能不准确,因为帖子编号可以很容易地从第一个查询更改到最后一个查询。是的,我看到了你的答案,它的优点仍然是抛出一个错误#的未定义方法'each'“似乎每种方法都有一个问题,只抓住一个item@Ian你能检查一下我上面的答案吗?嘿@Fabricioferitag你的答案是正确的,但是每次你添加一个帖子它都不会旋转,如果我需要抓取第八个,我就不能一直抓取数组中的最后一个。我通过下面的
类HomeController@posts=Post.all.order('created_at DESC')@onetofive=@posts[0..5]@six=@posts[6..6]@seven=@posts[7..7]@eight=@posts[8..8]@ninetoleven=@posts[9..11]end