没有路由匹配...缺少必需的键
No route matches ... missing required keys
我有两个控制器和型号 Projects
和 Schemas
。 Schemas
belongs_to
个项目。 Projects
has_many
schemas
。我正在寻找 http://localhost:3000/projects/SLUG-PROJECT/schemas/SLUG-SCHEMA.
以下是我的 SchemaController
代码:
class Projects::SchemasController < ApplicationController
before_action :set_schema, only: [:show, :edit, :update, :destroy]
before_action :set_project, only: [:index, :show, :new, :edit, :update, :destroy]
def index
@schemas = Schema.all
end
def show
end
def new
@schema = Schema.new
end
def edit
end
def create
@schema = Schema.new(schema_params)
respond_to do |format|
if @schema.save
format.html { redirect_to project_url(@schema.project_id), notice: 'Schema was successfully created.' }
else
format.html { render :new }
end
end
end
def update
respond_to do |format|
if @schema.update(schema_params)
format.html { redirect_to project_url(@schema.project_id), notice: 'Schema was successfully updated.' }
else
format.html { render :edit }
end
end
end
def destroy
@schema.destroy
respond_to do |format|
format.html { redirect_to project_url(@schema.project_id), notice: 'Schema was successfully destroyed.' }
end
end
private
def set_schema
@schema = Schema.find(params[:id])
end
def set_project
@project = Project.friendly.find(params[:project_id])
end
def schema_params
params.require(:schema).permit(:number, :identification, :reference, :name, :description, :author, :controller, :priority, :notes, :status, :cycle, :slug, :project_id)
end
end
这是我的代码:
respond_to do |format|
if @schema.update(schema_params)
format.html { redirect_to project_url(@schema.project_id), notice: 'Schema was successfully updated.' }
else
format.html { render :edit }
end
它适用于索引和显示页面,但我在更新、编辑和销毁时收到以下错误:
ActionController::UrlGenerationError in Projects::SchemasController#update
No route matches {:action=>"show", :controller=>"projects", :id=>nil} missing required keys: [:id]
谁能帮我弄清楚这是怎么回事?
你要找的是嵌套路由。在这种情况下,您可以包含此路由声明:
resources :projects do
resources :schemas
end
除了 projects
的路由之外,此声明还将 schemas
路由到 SchemasController
。 schema
网址需要 project
:
/projects/:project_id/schemas/:id
这还将创建路由助手,例如 project_schemas_url
和 edit_project_schema_path
。这些助手将 Project
的实例作为第一个参数:project_schemas_url(@project)
.
并记住始终在现有 project
上实例化 schemas
,例如:
@project.schemas.build
我有两个控制器和型号 Projects
和 Schemas
。 Schemas
belongs_to
个项目。 Projects
has_many
schemas
。我正在寻找 http://localhost:3000/projects/SLUG-PROJECT/schemas/SLUG-SCHEMA.
以下是我的 SchemaController
代码:
class Projects::SchemasController < ApplicationController
before_action :set_schema, only: [:show, :edit, :update, :destroy]
before_action :set_project, only: [:index, :show, :new, :edit, :update, :destroy]
def index
@schemas = Schema.all
end
def show
end
def new
@schema = Schema.new
end
def edit
end
def create
@schema = Schema.new(schema_params)
respond_to do |format|
if @schema.save
format.html { redirect_to project_url(@schema.project_id), notice: 'Schema was successfully created.' }
else
format.html { render :new }
end
end
end
def update
respond_to do |format|
if @schema.update(schema_params)
format.html { redirect_to project_url(@schema.project_id), notice: 'Schema was successfully updated.' }
else
format.html { render :edit }
end
end
end
def destroy
@schema.destroy
respond_to do |format|
format.html { redirect_to project_url(@schema.project_id), notice: 'Schema was successfully destroyed.' }
end
end
private
def set_schema
@schema = Schema.find(params[:id])
end
def set_project
@project = Project.friendly.find(params[:project_id])
end
def schema_params
params.require(:schema).permit(:number, :identification, :reference, :name, :description, :author, :controller, :priority, :notes, :status, :cycle, :slug, :project_id)
end
end
这是我的代码:
respond_to do |format|
if @schema.update(schema_params)
format.html { redirect_to project_url(@schema.project_id), notice: 'Schema was successfully updated.' }
else
format.html { render :edit }
end
它适用于索引和显示页面,但我在更新、编辑和销毁时收到以下错误:
ActionController::UrlGenerationError in Projects::SchemasController#update
No route matches {:action=>"show", :controller=>"projects", :id=>nil} missing required keys: [:id]
谁能帮我弄清楚这是怎么回事?
你要找的是嵌套路由。在这种情况下,您可以包含此路由声明:
resources :projects do
resources :schemas
end
除了 projects
的路由之外,此声明还将 schemas
路由到 SchemasController
。 schema
网址需要 project
:
/projects/:project_id/schemas/:id
这还将创建路由助手,例如 project_schemas_url
和 edit_project_schema_path
。这些助手将 Project
的实例作为第一个参数:project_schemas_url(@project)
.
并记住始终在现有 project
上实例化 schemas
,例如:
@project.schemas.build