ruby-on-rails Rails重定向路由,即使在逻辑上它没有意义

qmb5sa22  于 2023-03-31  发布在  Ruby
关注(0)|答案(1)|浏览(119)

我有以下控制器

class DashboardController < ApplicationController
  before_action :authenticate_user!, except: [:show]
  before_action :set_user, only: [:show]
  def index
    @should_render_navbar = true
  end

  def show
    redirect_to dashboard_path if @user.nil?
  end

  private

  def set_user
    # localhost:3000/1
    @user = User.find_by_id(params[:id])
  end
end

有以下路线

Rails.application.routes.draw do
  devise_for :users
  get 'dashboard', to: 'dashboard#index'
  root 'dashboard#index'

  # Allows us to use user_path(user) to get the URL for a user's profile page
  get ':id', to: 'dashboard#show', as: :user
  # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

  # Defines the root path route ("/")
  # root "articles#index"
end

当我手动输入localhost:3000/2时的控制台输出

Started GET "/2" for ::1 at 2023-03-28 15:53:25 -0400
Processing by DashboardController#show as HTML
  Parameters: {"id"=>"2"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/dashboard_controller.rb:18:in `set_user'
Redirected to http://localhost:3000/dashboard
Completed 302 Found in 3ms (ActiveRecord: 0.3ms | Allocations: 637)

我不知道为什么我被重定向到 Jmeter 板,特别是因为参数找到了用户

mwkjh3gx

mwkjh3gx1#

哎呀,我在rails控制台中销毁了2个用户,我的第一个User的id为3。

相关问题