无处可去"ActiveRecord::RecordNotFound in PostsController#show "



好的,所以我正在尝试显示索引.html,而 RoR 正在向我显示一些问题......posts_controller的DEF秀...好?

所以索引.html.erb

<!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_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 https://github.com/BetterErrors/better_errors 为您提供了一种实时调试错误的好方法,也许您可以安装此 gem 并尝试一下,它将为您提供更好的错误页面并能够实时查看不同的变量。

另外,看起来问题应该出

在您的
<%= link_to post.title, post %>

它肯定需要看起来像这样

<%= link_to(post.title, post_path(post)) %> 

<%= link_to post.tilte, post_path(post) %>

如果这不起作用,您需要查看您的路线,看看您是否定义了显示页面的路线。

好吧,所以我忘记了一件基本的事情。

routes.rb中添加get 'posts/index后,一切正常。

最新更新