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
Sie senden wahrscheinlich keine params [: trip_id] mit Ihrer Anfrage –
In dieser Show-Aktion zeigt 'params.inspect' an, welche Parameter übergeben werden. 'trip_id' wird in Ihrem Fall nicht als Parameter übergeben. – toddmetheny
Sie müssen mehr Informationen zeigen, die relevant sind für: trip_id, damit wir Ihnen helfen können. – MTarantini