Ruby on rails Params[:id]返回ActiveRecord::RecordNotFound错误

Ruby on rails Params[:id]返回ActiveRecord::RecordNotFound错误,ruby-on-rails,ruby,activerecord,Ruby On Rails,Ruby,Activerecord,我需要帮助,找出当我试图通过对象的ID检索对象时,为什么会出现ActiveRecord::RecordNotFound错误。下面是我的错误和代码。请让我知道,如果有任何其他文件需要添加到这个职位。提前感谢您的帮助 错误 ActiveRecord::RecordNotFound in WikisController#show Couldn't find Wiki with 'id'=edit def show @wiki = Wiki.find(params[:id]) #Highli

我需要帮助,找出当我试图通过对象的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
控制器

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

您似乎正在访问
wiki/edit
URL,而不是
wiki/:id/edit
。确保在视图中正确生成链接。

确保在视图中有类似的内容(如果使用erb):

此外,您还可以在采取行动之前使用
缩短控制器

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
类WikisController
请添加您的路线。看来编辑路径是错误的。@Tucker我通常使用
find\u by(attrs\u hash)
来抢占这个错误。如果没有找到记录,它将返回nil,并允许您处理该情况。@Shani-刚刚添加了路由。这肯定是一个路由问题,因为我的URL正在生成
wikis/edit
,而不是
wikis/id/edit
。我在这里遗漏了什么?你能粘贴生成url的代码吗?你是对的,我只是注意到我的url是Wiki/edit,而不是其中的ID。我刚刚发布了路由。路由中缺少什么导致URL的id部分丢失?请跟我说说,我对Rails还是相当陌生的。你能在这里添加你的视图吗?这将有助于我们确定问题所在。这是缩短/清理控制器的一种非常棒的方法。我正在学习Rails,所以这对我来说是新的。谢谢你!至于我的错误,我刚刚意识到我的URL没有正确生成。我得到的是
wiki/edit
,而不是
wiki/id/edit