2017-10-17 6 views
-1

두 개의 컨트롤러와 모델 ProjectsSchemas이 있습니다. Schemasbelongs_to 프로젝트. Projectshas_manyschemas. http://localhost:3000/projects/SLUG-PROJECT/schemas/SLUG-SCHEMA을 찾고 있습니다. 이건 내 코드입니다일치하는 경로가 없습니다 ... 필수 키가 누락되었습니다

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 

:

다음은 내 SchemaController 코드입니다 그것은 지수와 쇼 페이지에 대한 작동

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

당신이 당신의 설정/routes.rb를 공유 안될까요? – dskecse

답변

0

찾고있는 것은 중첩 된 경로입니다. 이 경우이 경로 선언을 포함 할 수있다 : projects의 경로에 추가

resources :projects do 
    resources :schemas 
end 

을,이 선언 것 또한 SchemasController 노선 schemas.

/projects/:project_id/schemas/:id 

이 또한 project_schemas_urledit_project_schema_path 같은 라우팅 헬퍼를 만듭니다 다음 schema의 URL은 project이 필요합니다. 이 도우미는 Project의 인스턴스를 첫 번째 매개 변수로 사용합니다 (project_schemas_url(@project)).

그리고 항상 기존 projectschemas을 인스턴스화하는 것을 잊지 말 :

@project.schemas.build