ruby-on-rails - 找不到 '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>
解决方案
正如其他用户在评论中建议的那样,您的数据库中可能没有记录,id=1
在这种情况下,find
方法将引发ActiveRecord:RecordNotFound
.
推荐阅读
- python - 如何在 for 循环中生成具有不同名称的新数组?
- python - 从用户那里获取整数输入来计算 Python 函数
- python - 避免
和 被 Spacy 解析 - mysql - JavaFX 如何在另一个项目中使用我自己的 .jar 库?
- c - 如何修改 BFS 算法以在给定条件下找到 2 个顶点之间的路径?
- vue.js - 添加弹出模式以 vuetify vue.js
- python - Modifying dictionary inside function without return statement
- javascript - 有没有办法检查单元格中的单词?
- python - 为什么我们将对 numpy.random.random 的调用乘以数字并减去数字?
- python - Azure 中与 Aws SqS 最相似的队列服务是什么?