【问题标题】:ActiveRecord::RecordNotFound (Couldn't find User with 'id'=101)ActiveRecord::RecordNotFound(找不到 'id'=101 的用户)
【发布时间】:2020-06-09 19:02:20
【问题描述】:

所以,我想在我的应用中添加分页功能。当我想向用户展示时,它第一次工作,但后来它没有工作,我想展示我的帖子。他们指出问题出在我的控制器上,但我不知道如何解决。

users_controller.rb
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @microposts = @user.microposts.paginate(page: params[:page])
    # binding.pry
    # debugger
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      # Handle a successful save.
      log_in @user
      flash[:success] = "Welcome to the Tute 6!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def index
    #@users = User.all
    @users = User.paginate(page: params[:page])
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end
end
db/seeds.rb
User.create!(name: "Example User",
  email: "example@railstutorial.org",
  password:
  "foobar",
  password_confirmation: "foobar")
99.times do |n|
  name = Faker::Name.name
  email = "example-#{n+1}@railstutorial.org"
  password = "password"
  User.create!(name: name,
    email: email,
    password: password,
    password_confirmation: password)
end

users = User.order(:created_at).take(6)
50.times do
  content = Faker::Lorem.sentence(5)
  users.each { |user| user.microposts.create!(content: content) }
end

如果你能帮助我,谢谢。

【问题讨论】:

  • 令人尴尬的问题,但是我如何删除我已经提出的这个问题?

标签: ruby-on-rails pagination


【解决方案1】:

如果你创建了 99 个用户,你应该找不到用户 101。不知道你是使用 gem 还是自己编写了分页逻辑,但我怀疑问题在于你的记录计数太多。如果你刚刚实现了分页系统,我会废弃它并非常简单地转向 pagy(gen pagy),并且比弄清楚这个额外的用户数来自哪里更快地解决你的问题

祝你好运!

【讨论】:

    【解决方案2】:

    所以显然错误发生是因为我在执行 rails db:migrate:reset 和 rails db:seed 时仍在我的主页中。当我这样做时,它还有效地删除了我在登录时添加的数据。这就是他们感到困惑的原因。在我回到主页并再次登录后,用户和我的帐户就可以工作了。所以,是的,这很尴尬。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 2017-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多