Ruby on rails 我只想在我的rails应用程序的索引页面中显示用户创建的帖子,而不是所有帖子

Ruby on rails 我只想在我的rails应用程序的索引页面中显示用户创建的帖子,而不是所有帖子,ruby-on-rails,ruby,model-view-controller,Ruby On Rails,Ruby,Model View Controller,这是我的管理员: class PostsController < ApplicationController before_action :authenticate_user! def index @post = Post.all end def new @post = Post.new end def create @post = Post.new(set_post) @

这是我的管理员:

class PostsController < ApplicationController
    before_action :authenticate_user!

    def index
        @post = Post.all
    end

    def new
        @post = Post.new
    end

    def create
        @post = Post.new(set_post)
        @post.user_id = current_user.id

        if @post.save
            redirect_to post_path(@post)
        else
            redirect_to new_post_path
        end
    end
class PostsController
我的模型:

class Post < ActiveRecord::Base
    has_attached_file :image, styles: { medium: "400x400#" }
    validates_attachment_content_type :image, content_type: /\Aimage 
       \/.*\Z/
    belongs_to :user
end

class User < ActiveRecord::Base
    devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable
    has_many :posts
end
class Post
现在我要做的是在我的索引操作中,有所有的帖子,我只需要创建它的用户的帖子,所以当用户登录时,他 应该重定向到他自己的帖子而不是所有的帖子。我是rails的新手,如有任何建议,将不胜感激。我不知道在索引中放什么,在视图中添加什么。我曾使用Desive

这样做:

def index
  @post = current_user.posts
end

只需更改控制器的索引方法

def index
  @posts = Post.where(:user_id => current_user.id)
end

这将为您提供一组属于登录用户的帖子。还有一件事,请将索引中的对象从“post”重命名为“posts”,因为结果不是单个post。

您是否考虑过将
post.all
更改为更合适的查询,因为表单仍然需要用户验证?