2017-10-17 40 views
-1

我有两个控制器和型号ProjectsSchemasSchemasbelongs_to项目。 Projectshas_manyschemas。我正在寻找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] 

有人能帮我弄清楚发生了什么事吗?

+0

你介意分享你的config/routes.rb文件? – dskecse

回答

0

你在找什么是嵌套的路线。在这种情况下,你可以包括这条路线声明:

resources :projects do 
    resources :schemas 
end 

除了路线projects,这一声明也将路由schemasSchemasController。该schema网址需要project

/projects/:project_id/schemas/:id 

这也将创造路由佣工如project_schemas_urledit_project_schema_path。这些助手以Project的实例作为第一个参数:project_schemas_url(@project)

而且记得要经常实例schemas在现有project,说:

@project.schemas.build 
相关问题