Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何仅显示用户的帖子。不是别人_Ruby On Rails_Associations_Clearance - Fatal编程技术网

Ruby on rails 如何仅显示用户的帖子。不是别人

Ruby on rails 如何仅显示用户的帖子。不是别人,ruby-on-rails,associations,clearance,Ruby On Rails,Associations,Clearance,这个应用程序是为导师设计的。当导师完成一堂课时,他们会填写一份课堂报告。然后索引页应该只显示他们的class_报告。 这是我的问题:我做了两个帐户,test1和test2。test1可以看到test1和test2的class_报告,但是test2不能看到任何帖子,甚至不能看到它们自己的帖子。它甚至说一篇文章是test1在test2创建它时创建的 我很确定类报告控制器的索引部分或创建部分出现了问题,但我不完全确定tbh。我认为它也可能出现在模型中 class_reports_controller.

这个应用程序是为导师设计的。当导师完成一堂课时,他们会填写一份课堂报告。然后索引页应该只显示他们的class_报告。 这是我的问题:我做了两个帐户,test1和test2。test1可以看到test1和test2的class_报告,但是test2不能看到任何帖子,甚至不能看到它们自己的帖子。它甚至说一篇文章是test1在test2创建它时创建的

我很确定类报告控制器的索引部分或创建部分出现了问题,但我不完全确定tbh。我认为它也可能出现在模型中

class_reports_controller.rb

class ClassReportsController < ApplicationController
  before_action :require_login
  before_action :set_class_report, only: [:show, :edit, :update, :destroy]

  # GET /class_reports
  # GET /class_reports.json
  def index
    @class_report = current_user.class_reports
  end

def create
    @class_report = ClassReport.new(class_report_params)
    @class_report.user = User.first

    respond_to do |format|
      if @class_report.save
        format.html { redirect_to @class_report, notice: 'Class report was successfully created.' }
        format.json { render :show, status: :created, location: @class_report }
      else
        format.html { render :new }
        format.json { render json: @class_report.errors, status: :unprocessable_entity }
      end
    end
  end
型号:

class_report.rb

class ClassReport < ApplicationRecord
  belongs_to :user
end

user.rb

class User < ApplicationRecord
  include Clearance::User
  has_many :class_reports
  before_save { self.email = email.downcase }
end


您的创建操作中有错误,在这一行:

@class_report.user=user.first

并将其更改为:

@class\u report.user=当前用户

所以第一行的问题是,所有报告都是创建的,并且总是链接到第一个用户,这就是为什么其他用户没有报告。通过更改为第二行,我们创建了一个报告并链接到登录用户current_user,因此该报告将被创建并分配给登录并创建报告的用户


希望能有所帮助。

@bendub89如果这个问题解决了你的问题,你可以接受。谢谢你让我知道。完成!