我需要幫助確定爲什麼當我嘗試通過它的ID檢索對象時出現ActiveRecord :: RecordNotFound錯誤。以下是我的錯誤和代碼。請讓我知道是否有任何其他文件需要添加到這篇文章。預先感謝您的幫助!Params [:id]返回ActiveRecord :: RecordNotFound錯誤
錯誤
ActiveRecord::RecordNotFound in WikisController#show
Couldn't find Wiki with 'id'=edit
def show
@wiki = Wiki.find(params[:id]) #Highlighted line within error
authorize @wiki
end
控制器
class WikisController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
def index
@wikis = Wiki.visible_to(current_user)
authorize @wikis
end
def new
@wiki = Wiki.new
authorize @wiki
end
def create
@wiki = current_user.wikis.create(wiki_params)
authorize @wiki
if @wiki.save
flash[:notice] = "Wiki was saved."
redirect_to @wiki
else
flash.now[:alert] = "Error saving Wiki. Try again."
render :new
end
end
def show
@wiki = Wiki.find(params[:id])
authorize @wiki
unless @wiki.private == nil
flash[:alert] = "You must be signed in to view private topics."
redirect_to new_session_path
end
end
def edit
@wiki = Wiki.find(params[:id])
authorize @wiki
end
def update
@wiki = Wiki.find(params[:id])
authorize @wiki
if @wiki.update_attributes(wiki_params)
flash[:notice] = "Wiki was updated."
redirect_to @wiki
else
flash.now[:alert] = "Error saving the Wiki. Try again."
render :edit
end
end
def destroy
@wiki = Wiki.find(params[:id])
authorize @wiki
if @wiki.destroy
flash[:notice] = "\"#{@wiki.title}\" was deleted successfully."
redirect_to root_path
else
flash.now[:alert] = "Error deleting Wiki. Try again."
render :show
end
end
private
def wiki_params
params.require(:wiki).permit(:title, :body, :role)
end
end
路線
Rails.application.routes.draw do
resources :wikis
resources :charges, only: [:new, :create]
devise_for :users
resources :users, only: [:update, :show] do
post 'downgrade'
end
get 'welcome/index'
get 'welcome/about'
root 'welcome#index'
end
請添加您的路線絕對縮短你的控制器。它似乎編輯路線是錯誤的。 – Shani
@Tucker我通常使用'find_by(attrs_hash)'搶佔這個錯誤。如果沒有找到記錄,它將返回零,並允許您處理該案例。 –
@Shani - 剛剛添加了路線。這絕對是路由問題,因爲我的網址正在生成「維基/編輯」而不是「維基/編號/編輯」。我在這裏錯過了什麼? – Tucker