我正在创建一个示例应用程序但是我在SubtopicsController中遇到错误 ActiveRecord :: RecordNotFound #edit无法找到没有ID的Subtopic 在浪费了一小时后,我发现传递编辑动作的请求参数是。
Request Parameters:
{"maintopic_id"=>"1",
"format"=>"3"}
但我想传递像
这样的参数Request Parameters:
{"maintopic_id"=>"1",
"id"=>"3"}
以下是我的代码
maintopics控制器
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
maintopics show page
<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
请帮助我理解为什么会这样?为什么参数没有正确传递?。
答案 0 :(得分:0)
您使用不正确的路径进行编辑。尝试:
edit_maintopic_subtopic_path(@maintopic, subtopic)
更新:
resources :maintopics do
resources :subtopics
end