简体   繁体   English

代码学校截屏视频-从零开始使用Rails应用程序-第1部分错误:找不到带有“ id”的旅程=

[英]Code School Screencast - Rails App From Scratch - Part 1 Error: Couldn't find Trip with 'id'=

Following codeschool.com's ruby screencast on making an app and ran into this error. 跟随codeschool.com的ruby截屏视频制作一个应用程序时,遇到了此错误。

Full error is 完全错误是

ActiveRecord::RecordNotFound in DestinationsController#show ActiveRecord :: RecordNotFound在DestinationsController#show中

Couldn't find Trip with 'id'= 找不到带有“ id” =的旅程

The error applies to the @trip instance below 该错误适用于以下@trip实例

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

Here is the applicable code from the destinations_controller.rb: 以下是destinations_controller.rb中的适用代码:

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

Here is the Trip model 这是旅行模型

class Trip < ActiveRecord::Base
    has_many :destinations
end

And the Destination model 和目的地模型

class Destination < ActiveRecord::Base

        belongs_to :trip
    end

Routes 路线

Rails.application.routes.draw do
  resources :destinations

  resources :trips do 
    resources :destinations
  end

  root to: 'trips#index'

Any help is greatly appreciated. 任何帮助是极大的赞赏。 :) :) :) :) :) :)

Update 1 : From log files 更新1 :从日志文件

Started GET "/destinations/4" for ::1 at 2016-03-31 00:50:08 +0900 Processing by DestinationsController#show as HTML Parameters: {"id"=>"4"} 在2016-03-31 00:50:08 +0900处以:: 1的形式开始:: 1:1的GET“ / destinations / 4”,由DestinationsController#show处理为HTML参数:{“ id” =>“ 4”}

[1m[35mDestination Load (0.6ms)[0m SELECT "destinations".* FROM "destinations" WHERE "destinations"."id" = ? [1m [35m目标负载(0.6ms)[0m选择“目的地”。*从“目的地”,在“目的地”。“ id” =? LIMIT 1 [["id", 4]] LIMIT 1 [[“ id”,4]]

[1m[36mTrip Load (0.3ms)[0m [1mSELECT "trips".* FROM "trips" WHERE "trips"."id" = ? [1m [36m旅行负荷(0.3ms)[0m [1mSELECT“行程”。*在“行程”中,“行程”。“ id” =? LIMIT 1[0m [["id", nil]] LIMIT 1 [0m [[“ id”,nil]]

Completed 404 Not Found in 20ms (ActiveRecord: 1.8ms) 20ms内完成404找不到(ActiveRecord:1.8ms)

ActiveRecord::RecordNotFound (Couldn't find Trip with 'id'=): ActiveRecord :: RecordNotFound(找不到带有'id'=的旅程):
app/controllers/destinations_controller.rb:14:in `show'* app / controllers / destinations_controller.rb:14:在`show'*中

Update 2 : the destinations_controller in its entirety. 更新2 :完整的destinations_controller。

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

In your routes you currently have a nested route for destinations: 在您的路线中,您当前有一个通往目的地的嵌套路线:

resources :trips do 
  resources :destinations
end

This means a destination is expected to be accessed in the context of its trip. 这意味着在旅行途中可以访问目的地。

eg GET /trips/1/destinations/1.json where you'll have a trip_id parameter for the trip and an id parameter for the id of the destination. 例如GET /trips/1/destinations/1.json ,其中您将有一个trip_id参数用于旅行,一个id参数用于目的地的ID。

You're also defining an non-nested route for destinations: 您还要为目的地定义非嵌套路线:

resources :destinations

but your DestinationController's show action assumes the nested version is being used when it does: 但您的DestinationController's show动作假定嵌套版本在使用时正在使用:

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

Have a check that the GET request matches what's being shown in the screencast - or post a link to the exact screencast you're following. 检查GET请求是否与截屏中显示的内容匹配-或将链接发布到您正在跟踪的确切截屏中。

Change the show action to this: 将显示操作更改为:

def show
  @trip = @destination.trip
end

Edit: Removed @destination assignment here because of the before_action running set_destination. 编辑:由于运行set_destination的before_action,在此处删除了@destination分配。

The Destination model has one Trip : Destination模型有一次Trip

class Destination < ActiveRecord::Base
  belongs_to :trip
end

Since you're setting the @destination because id is actually passed over, you can just get @trip through association. 由于您设置@destination是因为id实际上是经过传递的,因此您可以通过关联获取@trip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM