0

サンプル アプリケーションを作成していますが、エラーActiveRecord::RecordNotFound in SubtopicsController#edit Couldn't find Subtopic without an ID が発生しました。これに何時間も費やした後、編集アクションで渡される要求されたパラメータが .

Request Parameters:
{"maintopic_id"=>"1",
 "format"=>"3"}

しかし、私は次のようなパラメータを渡したい

Request Parameters:
{"maintopic_id"=>"1",
 "id"=>"3"}

以下は私のコードです

メイントピック コントローラー

class MaintopicsController < ApplicationController
  before_action :set_maintopic, only: [:show, :edit, :update, :destroy]

  # GET /maintopics
  # GET /maintopics.json
  def index
    @maintopics = Maintopic.all
  end

  # GET /maintopics/1
  # GET /maintopics/1.json
  def show
    @maintopic = Maintopic.find(params[:id])
    @subtopics = @maintopic.subtopics.all
  end

  # GET /maintopics/new
  def new
    @maintopic = Maintopic.new
  end

  # GET /maintopics/1/edit
  def edit
    @maintopic = Maintopic.find(params[:id])
  end

  # POST /maintopics
  # POST /maintopics.json
  def create
    @maintopic = Maintopic.new(maintopic_params)

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

  # PATCH/PUT /maintopics/1
  # PATCH/PUT /maintopics/1.json
  def update
    respond_to do |format|
      if @maintopic.update(maintopic_params)
        format.html { redirect_to @maintopic, notice: 'Maintopic was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @maintopic.errors, status: :unprocessable_entity }
      end
    end
  end

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

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def maintopic_params
      params.require(:maintopic).permit(:text)
    end
end

サブトピック コントローラ

  def new
    @maintopic = Maintopic.find(params[:maintopic_id])
  end

  def create
    @maintopic = Maintopic.find(params[:maintopic_id])
    @subtopic = @maintopic.subtopics.create(params[:subtopic].permit(:heading, :body))
    redirect_to maintopics_path(@maintopic)
  end

  def edit
    @maintopic = Maintopic.find(params[:maintopic_id])
    @subtopic = Subtopic.find(params[:id])
  end

メイントピックショーページ

<p id="notice"><%= notice %></p>

<p>
  <strong>Text:</strong>
  <%= @maintopic.text %>
</p>

<h2>Subtopic</h2>

<% @subtopics.each do |subtopic| %>
  <p>
  <strong>Heading:</strong>
  <%= subtopic.heading %>
</p>
<p>
  <strong>Body:</strong>
  <%= subtopic.body %>
</p>
<%= link_to 'Edit Subtopic', edit_maintopic_subtopics_path(@maintopic, subtopic) %>
<% end %>
<br>
<%= link_to 'Add Subtopic', new_maintopic_subtopics_path(@maintopic) %> |
<%= link_to 'Edit', edit_maintopic_path(@maintopic) %> |
<%= link_to 'Back', maintopics_path %>

ルートファイル

Topic::Application.routes.draw do

  resources :maintopics do
    resource :subtopics
  end
end

私のレーキルートの出力は

 maintopic_subtopics POST   /maintopics/:maintopic_id/subtopics(.:format)      subtopics#create
 new_maintopic_subtopics GET    /maintopics/:maintopic_id/subtopics/new(.:format)  subtopics#new
edit_maintopic_subtopics GET    /maintopics/:maintopic_id/subtopics/edit(.:format) subtopics#edit
                         GET    /maintopics/:maintopic_id/subtopics(.:format)      subtopics#show
                         PATCH  /maintopics/:maintopic_id/subtopics(.:format)      subtopics#update
                         PUT    /maintopics/:maintopic_id/subtopics(.:format)      subtopics#update
                         DELETE /maintopics/:maintopic_id/subtopics(.:format)      subtopics#destroy
              maintopics GET    /maintopics(.:format)                              maintopics#index
                         POST   /maintopics(.:format)                              maintopics#create
           new_maintopic GET    /maintopics/new(.:format)                          maintopics#new
          edit_maintopic GET    /maintopics/:id/edit(.:format)                     maintopics#edit
               maintopic GET    /maintopics/:id(.:format)                          maintopics#show
                         PATCH  /maintopics/:id(.:format)                          maintopics#update
                         PUT    /maintopics/:id(.:format)                          maintopics#update
                         DELETE /maintopics/:id(.:format)                          maintopics#destroy
                    root GET    /                                                  maintopics#index

なぜこれが起こっているのか理解するのを手伝ってください。パラメータが正しく渡されないのはなぜですか?

4

1 に答える 1