2017-10-17 117 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 
相關問題