我是rails的新手,我正在创建一个类似于Esquire网站的博客,在那里,他们在页面顶部将最新的六个帖子排成一排,然后再往下一点,他们会有一个很大的帖子,继续帖子数组,所以它实际上是第7个帖子,然后再往下一点,你会有下三个帖子,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我在主页上将其命名为:
<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>发布于 2017-03-08 04:32:59
我认为你需要的是这样的东西:
@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发布于 2017-03-08 04:36:46
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))https://stackoverflow.com/questions/42656895
复制相似问题