2016-04-12 56 views
1

我需要幫助確定爲什麼當我嘗試通過它的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 
+1

請添加您的路線絕對縮短你的控制器。它似乎編輯路線是錯誤的。 – Shani

+0

@Tucker我通常使用'find_by(attrs_hash)'搶佔這個錯誤。如果沒有找到記錄,它將返回零,並允許您處理該案例。 –

+0

@Shani - 剛剛添加了路線。這絕對是路由問題,因爲我的網址正在生成「維基/編輯」而不是「維基/編號/編輯」。我在這裏錯過了什麼? – Tucker

回答

2

好像你正在訪問wikis/edit URL而不是wikis/:id/edit。確保您的鏈接在您的視圖中正確生成。

+0

你說得對,我只注意到我的網址是維基/編輯,而不是裏面的ID。我剛剛發佈了路線。 – Tucker

+0

好了,問題解決了嗎? – Uzbekjon

+0

我的路線中缺少什麼導致URL的id部分缺失?請和我一起,我對Rails還是比較新的。 – Tucker

0

確保在你看來,你有這樣的事情(如果使用ERB):

<%= link_to edit_wiki_path(@wiki) %>

您也可以使用before_action

class WikisController < ApplicationController 
    ... 
    before action :set_wiki, only: [:show, :edit, :update, :destroy] 

    ... 

    private 

    def set_wiki 
    # Now this is being set in your show, edit, update, destroy method 
    # Make sure to delete from the above mentioned methods 
    @wiki = Wiki.find(params[:id]) 
    end 
+1

這是一個縮短/清理控制器的好方法。我正在學習Rails,所以這對我來說是新的。感謝那!至於我的錯誤,我剛剛意識到我的網址沒有正確生成。我得到'wikis/edit'而不是'wiki/id/edit'。 – Tucker

相關問題