1

I generated a scaffold using the rails generator, and created the controller below.

When I try to check if it works, I get an error message that says:

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

I was getting the same error recently (question below) and the solution was to remove :index from the before action. However, this controller doesn't include that action in the 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

Can anyone see what's wrong?

Community
  • 1
  • 1
Mel
  • 2,481
  • 26
  • 113
  • 273

1 Answers1

1

/org_checklists/index corresponds to the show action in Rails, and it requires you to send a valid id to get the record from the database.

Since, you are passing index instead of id, so Rails is trying to use index to get a record form the database, and since it can't find one, so it gives you: ActiveRecord::RecordNotFound at /org_checklists/index

In order to rectify this, you need to pass a valid id like /org_checklists/5, and it will send you the show page for the record with id 5.

Note: if you are trying to access the index page, you just need to call /org_checklists/, and that's it; Rails will send you the index page, you do not need to explicitly mention index at the end. This is the essence to RESTful web services.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76