好吧,所以我试图显示index.html,RoR向我展示了某些问题……def show posts_controller中的显示……好吗?
所以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>
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]
,但仍然遇到相同的错误。
您能解释什么地方出了什么问题以及需要解决的问题吗?
答案 0 :(得分:1)
您可以发布所收到的错误吗?
一个疯狂的猜测,问题可能出在这行
<%= link_to post.title, post%>
如果是,请将其更改为
<%= link_to post.title, post_path(post) %>
答案 1 :(得分:0)
刚刚开始,甚至现在我发现在开发中,Better BetterErrors https://github.com/BetterErrors/better_errors为您提供了一种实时调试错误的好方法,也许您可以安装此gem并尝试一下,它将为您提供更好的错误页面以及实时查看不同var的功能。
同样,问题似乎出在您的
<%= link_to post.title, post %>
它肯定需要看起来像这样
<%= link_to(post.title, post_path(post)) %>
或
<%= link_to post.tilte, post_path(post) %>
如果这不起作用,则需要查看您的路线,并查看是否为显示页面定义了路线。
答案 2 :(得分:0)
好的,所以我忘了一个基本的东西。
在get 'posts/index
中添加routes.rb
后,一切正常。