ruby-on-rails - 不知从何处获取“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]
,还是报同样的错误。
你能解释一下什么是错的,什么需要解决吗?
解决方案
你能发布你得到的错误吗?
只是一个疯狂的猜测,问题可能出在这一行
<%= link_to post.title, post%>
如果是这样,请将其更改为
<%= link_to post.title, post_path(post) %>
推荐阅读
- sql - SQLite - 查找在另一行中引用的所有数据及其相关数据
- ubuntu - Running PHP in Nginx - PHP directory missing inside /run - ubuntu
- d3.js - 如何在导入的 SVG 上使用 d3.js Fisheye
- html - 似乎无法让我的收音机与引导程序内联
- sql - 嵌入式 SELECT 中的 WHERE 子句
- eclipse - 删除和添加 osgi 包
- c++ - A const & refers to a nonvolatile variable. The variable changes. Does the change invalidate the const &?
- ruby-on-rails - 仅在合适的条件下执行工作
- apache-spark - 将 RDD 转换为 Dataframe 后的错误:“java.lang.String 不是 int 架构的有效外部类型”
- node.js - Negating a backreference in regex