简体   繁体   English

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

[英]i only want to display posts created by user in index page in my rails app instead of all posts

This is my posts 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)
        @post.user_id = current_user.id

        if @post.save
            redirect_to post_path(@post)
        else
            redirect_to new_post_path
        end
    end

My models: 我的模特:

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

Now what I have to do is in my index action where there are all posts I need only the post of user who created it, so when user gets logged in he should be redirected to his own posts instead of all posts. 现在我需要做的是在索引操作中,其中所有帖子我只需要创建它的用户的帖子,因此,当用户登录时,应该将他重定向到他自己的帖子,而不是所有帖子。 I am new to rails, any suggestion would be appreciated. 我是Rails的新手,任何建议将不胜感激。 I don't know what to put in index and what to add in views. 我不知道该在索引中添加什么,以及在视图中添加什么。 I have used Devise 我用过Devise

This would do that: 这样可以做到:

def index
  @post = current_user.posts
end

Just change your controller's index method. 只需更改控制器的索引方法即可。

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

This will give you an array of posts belonging to the user logged in. And one more thing, please rename the object in index from 'post' to 'posts' since the result is not a single post. 这将为您提供一系列属于已登录用户的帖子。还有一件事,请将索引中的对象从“ post”重命名为“ posts”,因为结果不是单个帖子。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我的rails应用程序允许用户对帖子进行投票,如何使用户在索引页上看到“自己投票”的帖子? - My rails app let user vote on posts, how to make user see “unvoted by his own” posts at index page? 如何在我的帖子索引页面上添加复选框过滤器(Rails应用) - How do I add checkbox filters to my Posts index page (Rails app) 如何显示所有子类别的帖子,而不是“ .first” | Ruby on Rails - How to display all subcategory's posts instead of “.first” | Ruby on Rails 用户在Rails中发布到组页面 - Posts to a group page, by a user in Rails 在列出所有帖子的索引页面中的单个帖子上带有jQuery的Rails调用注释框 - Rails call comment box with jquery on single posts in a index page where all posts are listed 我希望我的帖子在Ruby on Rails旁显示 - I want my posts to show next to eachother Ruby on Rails Rails计算属于用户的所有帖子的页面视图 - Rails count page views of all the posts that belong to a user 我希望我的轮播仅出现在 Rails 应用程序的索引页中 - I want my carousel to be appeared in index page only in rails application Rails-使用Posts#Index视图仅显示“喜欢”的帖子 - Rails - Using Posts#Index View to show only 'Liked' Posts Ruby on Rails:devise:,我如何允许帖子/索引和帖子/显示操作对所有操作,但其他操作将被拒绝 - Ruby on rails: devise :, how can i allow posts/index and posts/show action to all but other actions will be denied
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM