Ruby on rails 错误ActiveRecord::在Rails中找不到RecordNotFound
ActiveRecord::RecordNotFound in PicsController#show 找不到id为1的图片 提取的源(第34行附近): 我不能解决这个错误 我的pics_控制器.rb如下所示Ruby on rails 错误ActiveRecord::在Rails中找不到RecordNotFound,ruby-on-rails,activerecord,ruby-on-rails-5.1,Ruby On Rails,Activerecord,Ruby On Rails 5.1,ActiveRecord::RecordNotFound in PicsController#show 找不到id为1的图片 提取的源(第34行附近): 我不能解决这个错误 我的pics_控制器.rb如下所示 class PicsController < ApplicationController before_action :find_pic, only: [:show, :edit, :update, :destroy] def index
class PicsController < ApplicationController
before_action :find_pic, only: [:show, :edit, :update, :destroy]
def index
end
def show
end
def new
@pic = Pic.new
end
def create
@pic = current_user.pics.build(pic_params)
if @pic.save
redirect_to @pic,notice: "Yesss! It was posted!"
else
render 'new'
end
end
private
def pic_params
params.require(:pic).permit(:title, :description)
end
def find_pic
@pic = Pic.find(params[:id]).permit(:title) rescue nil
end
end
更新:我运行了rake routes
,下面是输出
$ rake routes
Prefix Verb URI Pattern Controller#Action
pics GET /pics(.:format) pics#index
POST /pics(.:format) pics#create
new_pic GET /pics/new(.:format) pics#new
edit_pic GET /pics/:id/edit(.:format) pics#edit
pic GET /pics/:id(.:format) pics#show
PATCH /pics/:id(.:format) pics#update
PUT /pics/:id(.:format) pics#update
DELETE /pics/:id(.:format) pics#destroy
root GET / pics#index
你应该试试
@pic=pic.find(params[:id])
,这就足够了
%h1= @pic.title if @pic.present?
%p= @pic.description if @pic.present?
= link_to "Back", root_path
如果不存在pic,请尝试挽救错误。我对这个
感到困惑。允许在对象上,然后挽救无东西。为什么需要它
据我所知,.permit
仅允许在ActionController::Parameters
上使用
所以,也许你必须重写那篇find_pic
:
def find_pic
@pic = Pic.find(params[:id])
end
pics
表中没有记录如果打开控制台并尝试Pic.find(1),是否有该id的记录?请勿在问题中张贴图片。而是剪切并粘贴相关文本。其他有相同问题的人稍后可能会遇到这个问题,但是如果您的图像主机删除了该图像,这个问题将不再有意义。不再显示错误->ActiveRecord::RecordNotFound:找不到id=1的图片已工作!!谢谢。没有添加许可证,它又出现了一个错误。ActiveRecord::在PicsController#show中未找到RecordNot
%h1= @pic.title if @pic.present?
%p= @pic.description if @pic.present?
= link_to "Back", root_path
def find_pic
@pic = Pic.find(params[:id])
end