Ruby on rails 不知从何处获取“ActiveRecord::RecordNotFound in PostsControllershow”
好的,我试着显示index.html,RoR向我展示了一些东西。。。def显示在posts\U控制器中。。。好吧 所以index.html.erbRuby on rails 不知从何处获取“ActiveRecord::RecordNotFound in PostsControllershow”,ruby-on-rails,ruby,controller,Ruby On Rails,Ruby,Controller,好的,我试着显示index.html,RoR向我展示了一些东西。。。def显示在posts\U控制器中。。。好吧 所以index.html.erb <!DOCTYPE html> <html> <head> <title></title> </head> <body> <% @posts.each do |post| %> <div class="post_wrapper
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<% @posts.each do |post| %>
<div class="post_wrapper">
<h2 class="title"><%= link_to post.title, post%></h2>
<p class="date"><%= time_ago_in_words(post.created_at)%></p>
</div>
<% end %>
</body>
</html>
posts\u controller.rb
class PostsController < ApplicationController
def index
@posts = Post.all.order('created_at DESC')
end
def new
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
def show
@post = Post.find(params[:id])
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
我猜这是因为没有ID这样的属性,所以我将params[:ID]更改为params[:title],仍然得到相同的错误
你能解释一下什么地方出了问题,什么地方需要修复吗?你能把你遇到的错误贴出来吗 只是胡乱猜测,问题可能就在这一行
<%= link_to post.title, post%>
如果是,请将其更改为
<%= link_to post.title, post_path(post) %>
刚开始,甚至现在我已经发现,在开发中,gem BetterErrors为您提供了一种很好的实时调试错误的方法,也许您可以安装这个gem并尝试一下,它将为您提供更好的错误页面和查看不同变量的能力 而且,看起来问题应该出在你的
<%= link_to post.title, post %>
它一定要看起来像这样
<%= link_to(post.title, post_path(post)) %>
或
如果这不起作用,您需要查看您的路线,并查看是否定义了显示页面的路线 好吧,我忘了一件基本的事
在routes.rb中添加get“posts/index”后,一切正常。请检查错误日志。它是否像ActiveRecord::RecordNotFound:找不到没有ID的帖子或ActiveRecord::RecordNotFound:找不到ID为“”的用户?你能在地址栏中发布你的url吗?@ray所以它是ActiveRecord::RecordNotFound在PostsControllershow中,然后找不到ID为“”的帖子。@iGian@iGian that;这是我在控制台窗口中运行rails的所有内容:ActiveRecord::RecordNotFound找不到带有'id'的帖子=index:app/controllers/posts\u controller.rb:16:in show'在2018-12-29 13:53:08+0100 PostsControllershow作为HTML参数进行处理:{id=>index}Post Load 0.5ms从posts.id=?限度[[id,0],[LIMIT,1]]在4ms ActiveRecord中未找到完成的404:0.5ms`当然,错误为ActiveRecord::RecordNotFound in PostsControllershow,然后在下面找不到'id'=索引的帖子,顺便说一句,您建议的更改没有帮助:
<%= link_to post.tilte, post_path(post) %>