我有以下控制器
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 板,特别是因为参数找到了用户
1条答案
按热度按时间mwkjh3gx1#
哎呀,我在rails控制台中销毁了2个用户,我的第一个
User
的id为3。