2016-03-30 5 views
0

Folgende codeschool.com's Ruby Screencast auf die Erstellung einer App und lief in diesen Fehler.Codeschule Screencast - Rails App von Grund auf - Teil 1 Fehler: Konnte Reise mit "ID" nicht finden =

Voll Fehler ist

ActiveRecord::RecordNotFound in DestinationsController#show

Couldn't find Trip with 'id'=

Der Fehler auf die @trip Instanz gilt unter

GET /destinations/1.json 
def show 
@trip = Trip.find(params[:trip_id]) 

Hier ist der entsprechende Code aus dem destinations_controller.rb:

def show 
    @trip = Trip.find(params[:trip_id]) 
    @destination = Destination.find(params[:id]) 
    end 

Hier ist das Trip-Modell

class Trip < ActiveRecord::Base 
    has_many :destinations 
end 

und das Zielmodell

class Destination < ActiveRecord::Base 

     belongs_to :trip 
    end 

Routen

Rails.application.routes.draw do 
    resources :destinations 

    resources :trips do 
    resources :destinations 
    end 

    root to: 'trips#index' 

Jede Hilfe wird sehr geschätzt. :) :) :)

Update 1: Von Logfiles

Started GET "/destinations/4" for ::1 at 2016-03-31 00:50:08 +0900 Processing by DestinationsController#show as HTML Parameters: {"id"=>"4"}

[1m[35mDestination Load (0.6ms)[0m SELECT "destinations".* FROM "destinations" WHERE "destinations"."id" = ? LIMIT 1 [["id", 4]]

[1m[36mTrip Load (0.3ms)[0m [1mSELECT "trips".* FROM "trips" WHERE "trips"."id" = ? LIMIT 1[0m [["id", nil]]

Completed 404 Not Found in 20ms (ActiveRecord: 1.8ms)

ActiveRecord::RecordNotFound (Couldn't find Trip with 'id'=):
app/controllers/destinations_controller.rb:14:in `show'*

Update 2: der destinations_controller in seiner Gesamtheit.

class DestinationsController < ApplicationController 
    before_action :set_destination, only: [:show, :edit, :update, :destroy] 

    # GET /destinations 
    # GET /destinations.json 
    def index 
    @destinations = Destination.all 
    end 

    # GET /destinations/1 
    # GET /destinations/1.json 
    def show 
    Rails.logger.debug params.inspect 
    @trip = Trip.find(params[:trip_id]) 
    @destination = Destination.find(params[:id]) 
    end 

    # GET /destinations/new 
    def new 
    @trip = Trip.find(params[:trip_id]) 
    @destination = Destination.new 
    end 

    # GET /destinations/1/edit 
    def edit 
    @trip = Trip.find(params[:trip_id]) 
    @destination = Destination.find(set_destination) 
    end 

    # POST /destinations 
    # POST /destinations.json 
    def create 
    @trip = Trip.find(params[:trip_id]) 
    @destination = @trip.destinations.new(destination_params) 

    respond_to do |format| 
     if @destination.save 
     format.html { redirect_to trip_destination_path(@trip, @destination), notice: 'Destination was successfully created.' } 
     format.json { render :show, status: :created, location: @destination } 
     else 
     format.html { render :new } 
     format.json { render json: @destination.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /destinations/1 
    # PATCH/PUT /destinations/1.json 
    def update 
    respond_to do |format| 
     if @destination.update(destination_params) 
     format.html { redirect_to @destination, notice: 'Destination was successfully updated.' } 
     format.json { render :show, status: :ok, location: @destination } 
     else 
     format.html { render :edit } 
     format.json { render json: @destination.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /destinations/1 
    # DELETE /destinations/1.json 
    def destroy 
    @destination.destroy 
    respond_to do |format| 
     format.html { redirect_to destinations_url, notice: 'Destination was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_destination 
     @destination = Destination.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def destination_params 
     params.require(:destination).permit(:name, :description) 
    end 
end 
+3

Sie senden wahrscheinlich keine params [: trip_id] mit Ihrer Anfrage –

+1

In dieser Show-Aktion zeigt 'params.inspect' an, welche Parameter übergeben werden. 'trip_id' wird in Ihrem Fall nicht als Parameter übergeben. – toddmetheny

+1

Sie müssen mehr Informationen zeigen, die relevant sind für: trip_id, damit wir Ihnen helfen können. – MTarantini

Antwort

1

Ändern Sie die Show Aktion dazu:

def show 
    @trip = @destination.trip 
end 

Edit: Entfernt @destination Zuordnung hier wegen der before_action läuft set_destination.

Das Destination Modell hat ein Trip:

class Destination < ActiveRecord::Base 
    belongs_to :trip 
end 

Da Sie die @destination weil id sind Einstellung tatsächlich übergegangen ist, die Sie gerade @trip durch die Verbindung zu bekommen.

+0

danke! Es funktionierte! Ich versuche immer noch, meinen Kopf darum zu wickeln: _ "Da du das @ -Ziel einrichtest, weil die ID tatsächlich übergeben wird, kannst du @Ausflug durch die Assoziation bekommen." _ – Salamit

+1

Also wissen wir, dass der 'id' -Parameter ist übergeben wird, der Parameter 'trip_id' jedoch nicht. Wir verwenden 'id', um' @destination' innerhalb der 'show' Aktion (Methode) zu setzen. Da wir wissen, dass 'Destination' zu 'Trip' gehört, brauchen wir nicht 'trip_id'. Wir können zu der einzigen "Reise" gelangen, die "@ destination" durch Assoziation hat: "@ destination.trip". – MTarantini

1

In Ihrer Route haben Sie zur Zeit eine verschachtelte Route für Ziele:

resources :trips do 
    resources :destinations 
end 

Das bedeutet, ein Ziel im Rahmen seiner Reise zugegriffen werden soll.

z.B. GET /trips/1/destinations/1.json, wo Sie einen trip_id Parameter für die Reise und einen id Parameter für die ID des Ziels haben.

Sie sind auch eine nicht-verschachtelte Route für Ziele definieren:

resources :destinations 

aber Ihre DestinationController's show-Aktion übernimmt die verschachtelte Version verwendet wird, wenn es funktioniert:

@trip = Trip.find(params[:trip_id]) 
@destination = Destination.find(params[:id]) 

Haben Sie einen Scheck dass die GET Anfrage mit dem übereinstimmt, was im Screencast gezeigt wird - oder post einen Link zu dem genauen Screencast, dem du folgst.

+0

Danke mikej. https://www.codeschool.com/screencasts/rails-app-from-scratch-part-1 aber ich denke, dass es hinter einer Paywall ist. Buchen Sie es für den Fall, dass Sie ein Konto haben. – Salamit

Verwandte Themen