【问题标题】:Rails 4 - Couldn't find OrgChecklist with 'id'=indexRails 4 - 找不到带有 'id'=index 的 OrgChecklist
【发布时间】:2016-02-24 15:56:51
【问题描述】:

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

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

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

我最近遇到了同样的错误(下面的问题),解决方案是从before action 中删除:index。但是,此控制器在 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

谁能看出问题所在?

【问题讨论】:

    标签: ruby-on-rails path controller


    【解决方案1】:

    /org_checklists/index对应Rails中的show动作,它需要你发送一个有效的id才能从数据库中获取记录。

    由于您传递的是index 而不是id,所以Rails 尝试使用index 从数据库中获取记录,并且由于找不到,所以它给了您:@987654327 @

    为了纠正这个问题,你需要传递一个有效的id,比如/org_checklists/5,它会向你发送ID为5的记录显示页面。

    注意:如果你想访问index页面,你只需要调用/org_checklists/,就可以了; Rails 将向您发送索引页面,您无需在末尾明确提及index。这就是 RESTful Web 服务的本质。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-01
      • 1970-01-01
      • 2015-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多