0

我正在创建一个示例应用程序,但在 SubtopicsController#edit 中出现错误 ActiveRecord::RecordNotFound 在浪费了一个小时后找不到没有 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

我的rake routes输出是

 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

0

您使用不正确的路径进行编辑。尝试:

edit_maintopic_subtopic_path(@maintopic, subtopic)

更新:

resources :maintopics do
  resources :subtopics
end
于 2013-10-01T11:57:58.717 回答