Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Activerecord 不能';找不到没有ID的子主题_Activerecord_Ruby On Rails 4 - Fatal编程技术网

Activerecord 不能';找不到没有ID的子主题

Activerecord 不能';找不到没有ID的子主题,activerecord,ruby-on-rails-4,Activerecord,Ruby On Rails 4,我正在创建一个示例应用程序,但在Subtopics控件中发现一个错误ActiveRecord::RecordNotFound\edit无法找到没有ID的Subtopic 在浪费了几个小时之后,我发现正在传递编辑操作的请求参数是 Request Parameters: {"maintopic_id"=>"1", "format"=>"3"} 但我想传递参数,比如 Request Parameters: {"maintopic_id"=>"1", "id"=>"3"}

我正在创建一个示例应用程序,但在Subtopics控件中发现一个错误ActiveRecord::RecordNotFound\edit无法找到没有ID的Subtopic 在浪费了几个小时之后,我发现正在传递编辑操作的请求参数是

Request Parameters:
{"maintopic_id"=>"1",
 "format"=>"3"}
但我想传递参数,比如

Request Parameters:
{"maintopic_id"=>"1",
 "id"=>"3"}
下面是我的代码

main主题控制器

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

请帮助我理解为什么会发生这种情况?为什么参数传递不正确?

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

edit_maintopic_subtopic_path(@maintopic, subtopic)
更新:

resources :maintopics do
  resources :subtopics
end

我已经试过了,它给出了一个错误
未定义的方法
编辑#`的#主主题(u subtopic)路径(u path)。您需要使用
resources
而不是
resource
用于:子主题