无法找到' id' = 1

时间:2018-06-17 03:46:35

标签: ruby-on-rails

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

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

ActiveRecord:ListingController中的RecordNotFound#show
couldn&#t; t   找到列出' id' =#

的routes.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>

1 个答案:

答案 0 :(得分:0)

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