2017-03-07 73 views
0

我是新来的rails,我创建了一个博客,看起来像一个Esquire网站,他们有最新的六个职位在顶部该页面排列在一起,然后稍微向下一页,他们将有一个大的帖子本身继续发布​​数组,所以它将确实是第7篇文章,然后稍微向下,你会有下三个职位8-11等。Rails:选择博客的前6个帖子,然后选择第7个等等

我正在尝试使控制器成为一个阵列,在那里我可以先调用前六个项目,然后再分别在主页后面调用第七个项目。

帖子控制器:

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 

在哪里,我呼吁它在主页上:

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

你需要的是:1)第一个6个职位,2)第7个职位,和3)其余? –

+0

是的,正好和主页上显示 – Ian

+0

@Ian,请测试我的答案。 – fabriciofreitag

回答

0

我想你需要的是这样的:

@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

好的解决方案,但它可能不准确,因为帖子号可以很容易地从第一个查询改变到最后一个查询。 – fabriciofreitag

+0

是的,我见过你和它的更好 –

+0

它仍然抛出一个错误的“未定义的方法'每个'#后:0x007fb0f71ca7a0>'”似乎每种方法都有问题只抓取一个项目 – Ian

0
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)) 
相关问题