Rails 4 - 无法找到带有' id' = index

时间:2016-02-24 07:08:34

标签: ruby-on-rails path controller

我使用rails生成器生成了一个脚手架,并在下面创建了控制器。

当我尝试检查它是否有效时,我收到一条错误消息:

ActiveRecord::RecordNotFound at /org_checklists/index
Couldn't find OrgChecklist with 'id'=index

我最近收到了同样的错误(下面的问题),解决方法是从:index删除before action。但是,此控制器并未在before action中包含该操作。

Rails 4 error - rails Couldn't find User with 'id'=index

class OrgChecklistsController < ApplicationController
  before_action :set_org_checklist, only: [:show, :edit, :update, :destroy]

  # GET /org_checklists
  # GET /org_checklists.json
  def index
    @org_checklists = OrgChecklist.all
    authorize @org_checklists
  end

  # GET /org_checklists/1
  # GET /org_checklists/1.json
  def show
  end

  # GET /org_checklists/new
  def new
    @org_checklist = OrgChecklist.new
  end

  # GET /org_checklists/1/edit
  def edit
  end

  # POST /org_checklists
  # POST /org_checklists.json
  def create
    @org_checklist = OrgChecklist.new(org_checklist_params)

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

  # PATCH/PUT /org_checklists/1
  # PATCH/PUT /org_checklists/1.json
  def update
    respond_to do |format|
      if @org_checklist.update(org_checklist_params)
        format.html { redirect_to @org_checklist, notice: 'Org checklist was successfully updated.' }
        format.json { render :show, status: :ok, location: @org_checklist }
      else
        format.html { render :edit }
        format.json { render json: @org_checklist.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /org_checklists/1
  # DELETE /org_checklists/1.json
  def destroy
    @org_checklist.destroy
    respond_to do |format|
      format.html { redirect_to org_checklists_url, notice: 'Org checklist was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_org_checklist
      @org_checklist = OrgChecklist.find(params[:id])
      authorize @org_checklist
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def org_checklist_params
      params[:org_checklist].permit(:organisation_id, :payment_method, :interests_set )
    end
end

任何人都可以看到错误吗?

1 个答案:

答案 0 :(得分:1)

/org_checklists/index对应于Rails中的show操作,它要求您发送有效的id以从数据库中获取记录。

因为,你传递index而不是id,所以Rails正试图使用​​index从数据库中获取记录,因为它找不到一个,所以它会给你:ActiveRecord::RecordNotFound at /org_checklists/index

为了纠正此问题,您需要传递有效的id /org_checklists/5,它会向您发送ID为5的记录的显示页面。

注意:如果您尝试访问index页面,只需拨打/org_checklists/即可,就是这样; Rails会向您发送索引页面,您最后不需要明确提及index。这是RESTful Web服务的本质。