我已经按照Hartl的教程制作了带有标记系统的ToDoList,并且还借助了word guide和video。在临时修改代码时,我的development.log
中出现了此错误。有一个单独的页面,它将显示管理员用户。
development.log
Started GET "/users/admin" for 127.0.0.1 at 2019-01-26 18:55:57 +0800
Processing by UsersController#show as HTML
Parameters: {"id"=>"admin"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 0], ["LIMIT", 1]]
Completed 404 Not Found in 5ms (ActiveRecord: 0.2ms)
ActiveRecord::RecordNotFound (Couldn't find User with 'id'=admin):
app/controllers/users_controller.rb:8:in `show'
这是我的控制器代码:
MicropostsController
class MicropostsController < ApplicationController
before_action :logged_in_user, only: [:index, :show, :create, :destroy]
before_action :correct_user, only: :destroy
def index
@microposts = current_user.microposts
@microposts = @microposts.tagged_with(params[:tag]) if params[:tag]
end
def show
@micropost = Micropost.find(params[:id])
end
def create
@micropost = current_user.microposts.build(micropost_params)
if @micropost.save
flash[:success] = "Micropost created!"
redirect_to root_url
else
@feed_items = []
render 'static_pages/home'
end
end
def destroy
@micropost.destroy
flash[:success] = "You have deleted a task!"
redirect_to request.referrer || root_url
end
private
def micropost_params
params.require(:micropost).permit(:content, :tag_list, :tag,
{tag_ids: [] }, :tag_ids)
end
def correct_user
@micropost = current_user.microposts.find_by(id: params[:id])
redirect_to root_url if @micropost.nil?
end
end
SessionsController
class SessionsController < ApplicationController
def new
end
def create
@user = User.find_by(email: params[:session][:email].downcase)
if (@user && @user.authenticate(params[:session][:password]))
log_in @user
flash[:success] = "Welcome back, #{@user.name}!"
params[:session][:remember_me] == '1' ? remember(@user) : forget(@user)
redirect_back_or root_path
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
StaticPagesController
class StaticPagesController < ApplicationController
def home
if logged_in?
@new_micropost = Micropost.new
@feed_items = current_user.microposts.paginate(page: params[:page])
end
end
def help
end
def about
end
def contact
end
end
UsersController
class UsersController < ApplicationController
before_action :logged_in_user, only: [:edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
before_action :admin_user, only: [:destroy]
def show
@user = User.find(params[:id])
@microposts = @user.microposts.paginate(page: params[:page])
end
def new
@user = User.new
end
def index
@users = User.paginate(page: params[:page])
end
def create
@user = User.new(user_params)
if @user.save
log_in @user
flash[:info] = "Welcome to the to-do app, #{@user.name}"
redirect_to @user
else
render 'new'
end
end
def admin
@users = User.paginate(page: params[:page])
end
def destroy
a = User.find(params[:id]).name
User.find(params[:id]).destroy
flash[:success] = "#{a} has been deleted!"
redirect_to users_url
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
def edit
@user = User.find(params[:id])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Profile for @user.name has been updated"
redirect_to(@user)
else
flash[:danger] = "Update Failed."
render 'edit'
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password,
:password_confirmation, :admin)
end
def logged_in_user
unless logged_in?
store_location
flash[:danger] = "You are not logged in. Please log in."
redirect_to login_url
end
end
def correct_user
@user = User.find(params[:id])
if !current_user?(@user)
flash[:danger] = "You are not authorized to visit this page."
redirect_to(root_url)
end
end
end
Config / routes.rb并运行rake routes
Rails.application.routes.draw do
resources :users
resources :microposts
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/users/admin', to: 'users#admin'
get 'tags/:tag', to: 'microposts#index', as: :tag
root 'static_pages#home'
end
Prefix Verb URI Pattern Controller#Action
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
microposts GET /microposts(.:format) microposts#index
POST /microposts(.:format) microposts#create
new_micropost GET /microposts/new(.:format) microposts#new
edit_micropost GET /microposts/:id/edit(.:format) microposts#edit
micropost GET /microposts/:id(.:format) microposts#show
PATCH /microposts/:id(.:format) microposts#update
PUT /microposts/:id(.:format) microposts#update
DELETE /microposts/:id(.:format) microposts#destroy
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
signup GET /signup(.:format) users#new
POST /signup(.:format) users#create
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
users_admin GET /users/admin(.:format) users#admin
tag GET /tags/:tag(.:format) microposts#index
root GET / static_pages#home
有人知道如何解决问题吗?如果需要更多信息,请告诉我。非常感谢。
答案 0 :(得分:2)
您应在routes.rb
中移动以下行。
get '/users/admin', to: 'users#admin'
上方
resources :users
您遇到的问题是Rails路由系统将此路径-/users/admin
识别为与/users/:id
匹配,因此路由到users#show
操作。