如何使帖子评论在我的主页和 user/show 视图中可用?

How do I make the Posts comments available in my homepage and user/show view?

这是我的主控制器:

class HomeController < ApplicationController
    def home
        if logged_in?
            @post  = current_user.posts.build
            @feed_items = current_user.feed.paginate(page: params[:page])
        end
    end

    def about
    end

    def privacy
    end

    def terms
    end
end

这是我的评论控制器:

class CommentsController < ApplicationController
    def create
        @post = Post.find(params[:post_id])
        @comment = @post.comments.create(comment_params)
        redirect_to root_path
    end

    private
        def comment_params
            params.require(:comment).permit(:author_name, :body)
        end
    end

我的 post 控制器:

class PostsController < ApplicationController
    before_action :logged_in_user, only: [:create, :destroy]

    def create
        @post = current_user.posts.build(post_params)
        if @post.save
            flash[:success] = "Post created!"
            redirect_to root_url
        else
            @feed_items = []
            render 'home/home'
        end
    end

    def destroy
    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, :body, :picture)
       end
   end

我的用户模型:

class User < ActiveRecord::Base
    attr_accessor :remember_token
    before_save { self.email = email.downcase }
    has_many :posts, dependent: :destroy
    has_many :comments
    has_many :active_relationships, class_name:  "Relationship",
                              foreign_key: "follower_id",
                              dependent:   :destroy

    has_many :passive_relationships, class_name:  "Relationship",
                               foreign_key: "followed_id",
                               dependent:   :destroy
.............................................
.............................................
end

我的post模特:

class Post < ActiveRecord::Base
    belongs_to :user
    has_many :comments
    default_scope -> { order(created_at: :desc) }
    mount_uploader :picture, PictureUploader
    validates :user_id, presence: true
    validates :body, presence: true, length: { minimum:40 }   
end

如何确保 posts 和他们的评论都可以在主页(与主页视图对应的 homeController)和用户#show 中访问?我能够访问和查看主页视图中的 posts 和用户#show,但我无法访问评论。

最简单的方法。

用户控制器#index

@posts = current_user.posts

在节目中html

render @posts

为了正确呈现帖子,您必须有一个 _post.html.erb 以便 rails 知道如何呈现它们。