显示您关注的用户的帖子?

时间:2014-08-20 14:25:18

标签: ruby-on-rails ruby-on-rails-4

我想在流页面上显示我关注的用户的帖子。

我有一个帖子控制器,显示来自任何用户的所有帖子。我再次只希望我关注的用户的帖子显示在流页面上。我创建了一个名为“Stream”的新控制器。我正在努力让我关注的用户帖子显示在我的流索引上。

提前谢谢。

流控制器

class StreamController < ApplicationController
 before_action :set_post, only: [:show, :edit, :update, :destroy]
 before_action :correct_user, only: [:edit, :update, :destroy]
 before_action :authenticate_user!, except: [:index, :show]

 def index
    @posts = Post.where(follower_id: current_user.id, followed_id: current_user.id)
 end 

end

流索引

<div class="page-header">
  <center><strong><h1> Stream Page </h1></strong></center>
</div>

后置控制器

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]
  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @posts = Post.all.order("created_at DESC").paginate(:page => params[:page], :per_page => 40)
  end


  def show
  end


  def new
    @post = current_user.posts.build
  end


  def edit
  end


  def create
    @post = current_user.posts.build(post_params)

    respond_to do |format|
      if @post.save
        format.html { redirect_to @post, notice: 'Post was successfully created.' }
        format.json { render action: 'show', status: :created, location: @post }
      else
        format.html { render action: 'new' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end


  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end


  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url }
      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

    def correct_user
      @post = current_user.posts.find_by(id: params[:id])
      redirect_to posts_path, notice: "Not authorized to edit this post" if @post.nil?
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:description, :image)
    end

    end

用户控制器

class UsersController < ApplicationController

  before_action :correct_user, only: [:edit, :update, :destroy]
  before_action :authenticate_user!, except: [:index, :show]
  before_action :admin_user,     only: :destroy


   def following
    @title = "Following"
    @user = User.find(params[:id])
    @users = @user.followed_users.paginate(page: params[:page])
    render 'show_follow'
   end

  def followers
    @title = "Followers"
    @user = User.find(params[:id])
    @users = @user.followers.paginate(page: params[:page])
    render 'show_follow'
  end


  def index
    @users = User.paginate(page: params[:page], :per_page => 20)
  end


  def show
    @user = User.find(params[:id])

    if @user 

      @posts = @user.posts.order("updated_at DESC")
      render actions: :show
    else
      render file: 'public/404', status: 404, formats: [:html]
  end  
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "Your account has been deleted."
    redirect_to root_path
  end

  def correct_user
      @user = User.find(params[:id])
      redirect_to root_path 
  end

  def admin_user
      redirect_to root_path unless current_user.admin?
  end

end

关系控制器

class RelationshipsController < ApplicationController

  before_action :authenticate_user!, except: [:index, :show]


def create
    @user = User.find(params[:relationship][:followed_id])
    current_user.follow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

  def destroy
    @user = Relationship.find(params[:id]).followed
    current_user.unfollow!(@user)
    respond_to do |format|
      format.html { redirect_to @user }
      format.js
    end
  end

end

移植

class CreateRelationships < ActiveRecord::Migration
  def change
    create_table :relationships do |t|
      t.integer :follower_id
      t.integer :followed_id

      t.timestamps
    end
    add_index :relationships, :follower_id
    add_index :relationships, :followed_id
    add_index :relationships, [:follower_id, :followed_id], unique: true
  end
end

3 个答案:

答案 0 :(得分:3)

<强>关系

我个人使用范围与协会:

#app/models/user.rb
Class User < ActiveRecord::Base
   has_many :posts

   has_many :subscribed, class_name: "Relationship", foreign_key: "followed_id"
   has_many :followers, class_name: "Relationship", foreign_key: "follower_id"
end

#app/models/post.rb
Class Post < ActiveRecord::Base
   belongs_to :user
   scope :subscribed, ->(followers) { where user_id: followers }
end

#app/models/relationship.rb
Class Relationship < ActiveRecord::Base
   #fields id | user_id | follower_id | created_at | updated_at
   belongs_to :user
end

这将使您能够调用以下内容:

#app/controllers/stream_controller.rb
Class StreamController < ApplicationController
   def index
      @posts = Post.subscribed current_user.followers
   end
end

-

<强>替代

另一种选择如下:

#app/views/stream/index.html.erb
<% current_user.subscribed.each do |followed| %>
    <% followed.posts.each do |post| %>
       <%= post.title %>
    <% end %>
<% end %>

-

虽然我不确定这是否会起作用,但这就是我创建您正在寻找的功能的方式。从本质上讲,你必须能够通过&#34;相关的&#34;用户进行查询调用,然后返回属于以下关系的对象

答案 1 :(得分:0)

流控制器中的post变量需要是视图中可用的实例变量@posts,并匹配视图中的@posts实例变量。< / p>

答案 2 :(得分:0)

我想应该看看@posts = Post.subscribed @ user.following而不是&#34; current_user.followers&#34;。这可以解决您显示关注您的人而不是显示您关注的用户的问题。