2017-10-17 3 views
-1

J'ai deux contrôleurs et modèles Projects et Schemas. Schemasbelongs_to projets. Projectshas_manyschemas. Je cherche http://localhost:3000/projects/SLUG-PROJECT/schemas/SLUG-SCHEMA.Aucune correspondance correspond à ... clés manquantes requises

Après mon code 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 

Voici mon code:

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 

Il fonctionne pour les pages d'index et de montrer, mais je reçois l'erreur suivante pour mettre à jour, modifier et détruire :

ActionController::UrlGenerationError in Projects::SchemasController#update 

No route matches {:action=>"show", :controller=>"projects", :id=>nil} missing required keys: [:id] 

Quelqu'un peut-il m'aider à comprendre ce qui se passe?

+0

Pourriez-vous partager votre config/routes.rb? – dskecse

Répondre

0

Ce que vous cherchez, ce sont des routes imbriquées. Dans ce cas, vous pouvez inclure cette déclaration d'itinéraire:

resources :projects do 
    resources :schemas 
end 

En plus des routes pour projects, cette déclaration sera également la route schemas à un SchemasController. Les schema URL nécessitent une project:

/projects/:project_id/schemas/:id 

Cela permettra également de créer des aides de routage tels que project_schemas_url et edit_project_schema_path. Ces aides prennent une instance de Project comme premier paramètre: project_schemas_url(@project).

Et rappelez-vous toujours instancier schemas sur un existant project, disent:

@project.schemas.build