首页 > 解决方案 > 不知从何处获取“ActiveRecord::RecordNotFound in PostsController#show”

问题描述

好的,所以我正在尝试显示 index.html,而 RoR 向我展示了一些问题...在 posts_controller 中显示 def... 好吗?

所以 index.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>

post_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],还是报同样的错误。

你能解释一下什么是错的,什么需要解决吗?

标签: ruby-on-railsrubycontroller

解决方案


你能发布你得到的错误吗?

只是一个疯狂的猜测,问题可能出在这一行

<%= link_to post.title, post%>

如果是这样,请将其更改为

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

推荐阅读