首页 > 解决方案 > 找不到 'id'=1

问题描述

在添加代码之前它可以正常工作(可以显示页面)

# listings_controller.rb
def show
  @lsiting = Listing.find(params[:id])
end

ActiveRecord:RecordNotFound in ListingController#show
找不到带有 'id'= 的列表

路由.rb

Rails.application.routes.draw do

  resources :categories do
        resources :subcategories
  end

  resources :listings



  root 'categories#index'
  match '/help', to: 'pages#help', via: :get


end

# listings_controller.rb

class ListingsController < ApplicationController

    def new
        @listing = Listing.new
    end


    def create
      @listing = Listing.new(listing_params)

      @listing.save
       redirect_to root_path

    end


    def show
     @listing = Listing.find(params[:id])
    end

    private

    def listing_params
      params.require(:listing).permit(:title, :description, :city, :state, :zipcode)
    end

end

<!-- show.html.erb -->


<div class="topbar">
  <div class="container">
    <div class="vertical-center">
      rubyslist > jobs > accounting
    </div>
  </div>
</div>
<div class="container">
  <div>
    <button type="button">reply</button>
    posted
    <h1 class="listing-header"> <%= @listing.title %> </h1>
    <div class="box">
      <p>test</p>
    </div>
    <p> 
  </div>
  <footer>
    <p>post id: </p>
    <p>posted </p>
  </footer>
</div>

标签: ruby-on-rails

解决方案


正如其他用户在评论中建议的那样,您的数据库中可能没有记录,id=1在这种情况下,find方法将引发ActiveRecord:RecordNotFound.


推荐阅读