Ruby on rails 未定义的方法`销毁';对于nil:NilClass(rails)

Ruby on rails 未定义的方法`销毁';对于nil:NilClass(rails),ruby-on-rails,Ruby On Rails,我得到这个错误: CycleRoadsController中的命名错误#销毁 nil:NilClass(rails)的未定义方法“destroy” 这是来自控制器的代码,它们包含一个方法“destroy”: respond_to do |format| if @cycle_road.save format.html { redirect_to @cycle_road, notice: 'Cycle road was successfully creat

我得到这个错误:

CycleRoadsController中的命名错误#销毁

nil:NilClass(rails)的未定义方法“destroy”

这是来自控制器的代码,它们包含一个方法“destroy”:

        respond_to do |format|
      if @cycle_road.save
        format.html { redirect_to @cycle_road, notice: 'Cycle road was successfully created.' }
        format.json { render action: 'show', status: :created, location: @cycle_road }
      else
        format.html { render action: 'new' }
        format.json { render json: @cycle_road.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    respond_to do |format|
      if @cycle_road.update(cycle_road_params)
        format.html { redirect_to @cycle_road, notice: 'Cycle road was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @cycle_road.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /cycle_roads/1
  # DELETE /cycle_roads/1.json
  def destroy
    @cycle_road.destroy
    respond_to do |format|
      format.html { redirect_to cycle_roads }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_cycle_road
      @cycle_road = CycleRoad.find(params[:id])
    end

    def cycle_road_params
      params.require(:cycle_road).permit(:name, :begin, :finish, :km, :description)
    end
end

有人知道怎么回事吗?

在调用实例变量
@cycle\u road
之前,先设置实例变量
@cycle\u road

目前它的
nil
,因为错误清楚地表明
未定义的方法“destroy”用于nil:NilClass

根据共享控制器代码,您需要在
set\u cycle\u road

class CycleRoadController < ApplicationController
  before_action :set_cycle_road, only: [:show, :edit, :update, :destroy]
  ## ...                                                         ^
  ##                                                          Add this
end
class CycleRoadController

在调用
destroy
操作之前,此回调将负责设置
@cycle\u road
实例变量。

如前所述,@cycle\u road似乎为零。在采取行动之前,请确保您有:

class CycleRoadController < ApplicationController
  ...
  before_action :set_cycle_road
  ...

你们能发布控制器的所有代码吗?我把控制器的所有代码都放在上面了。Kirti Thorat说:你们试图删除一个不存在的项目。
before_action :set_cycle_road, only: [:show, :edit, :update, :destroy]