【发布时间】:2018-05-29 21:56:14
【问题描述】:
我是 Rails 的初学者,正在尝试创建一个 Reddit 类型的博客页面。目前,我正在研究嵌套的 cmets,以便用户能够回复 cmets。在我的应用程序中,我有论坛、帖子和 cmets。创建帖子和论坛效果很好,在我尝试嵌套回复之前还发布了 cmets。我的代码如下所示(论坛和用户模型/控制器已被省略):
我的评论模型:
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
belongs_to :user
has_many :comments, as: :commentable
end`
我的帖子模型:
class Post < ApplicationRecord
belongs_to :forum
belongs_to :user
has_many :comments, as: :commentable, dependent: :destroy
end
我的posts_controller:
class PostsController < ApplicationController
def index
@posts = Post.all
end
def show
@post = find_post
end
def new
@post = Post.new
end
def edit
@post = find_post
end
def create
@forum = Forum.find(params[:forum_id])
@post = @forum.posts.create(post_params.merge(user_id: current_user.id))
redirect_to forum_path(@forum)
end
def update
@post = find_post
if @post.update(post_params)
redirect_to forum_path
else
render 'show'
end
end
def destroy
@post = find_post
@post.destroy
redirect_to forum_path
end
private
def post_params
params.require(:post).permit(:title, :content)
end
private
def find_post
@forum = Forum.find(params[:forum_id])
@forum.posts.find(params[:id])
end
end
我的 cmets_controller:
class CommentsController < ApplicationController
before_action :find_commentable
def index
@comments = Comment.all
end
def new
@comment = Comment.new
end
def create
@comment = @commentable.comments.create(comment_params.merge(user_id: current_user.id))
if @comment.save
@post = Post.find_by_id(params[:post_id])
redirect_to forum_post_path(@post.forum_id, @post), notice: 'Your comment was successfully posted!'
else
redirect_to forum_post_path(@post.forum_id, @post), notice: 'error :('
end
end
private
def comment_params
params.require(:comment).permit(:content)
end
private
def find_commentable
@commentable = Comment.find_by_id(params[:comment_id]) if params[:comment_id]
@commentable = Post.find_by_id(params[:post_id]) if params[:post_id]
end
end
我的观点/cmets/_comment:
<li>
<%= comment.content %>
<small>Submitted <%= time_ago_in_words(comment.created_at) %> ago</small>
<h2>Add a Reply:</h2>
<%= form_with(model: [ comment, comment.comments.build ], local: true) do |form| %>
<p>
<%= form.text_area :content, placeholder: "Add a Reply" %><br/>
<%= form.submit %>
</p>
<% end %>
<ul>
<%= render partial: 'comments/comment', collection: comment.comments if comment.comments.any? %>
</ul>
<%= link_to 'Back', forum_post_path(@forum, @post) %>
我的观点/帖子/节目:
<small>Submitted <%= time_ago_in_words(@post.created_at) %> ago</small>
<h3>Comments</h3>
<ul>
<%= render(partial: 'comments/comment', collection: @post.comments) if @post.comments.any?%>
</ul>
<h2>Add a comment:</h2>
<%= form_with(model: [ @post, @post.comments.build ], local: true) do |form| %>
<%= form.text_field :content, placeholder: "Add a comment" %><br/>
<%= form.submit "Add Comment" %>
<% end %>
<%= link_to 'Back', forum_path(@forum) %>
<%= link_to 'All posts', forum_posts_path(@post.forum_id) %>
<%= link_to 'All comments', post_comments_path(@post) %>
</li>
我的路线:
resources :posts do
resources :comments
end
resources :comments do
resources :comments
end
我遇到的第一个错误是,每次我在view/posts/show 中提交评论时,评论都不会创建。现在,views/cmets/_comment 解除了错误:
Showing /example/app/views/comments/_comment.html.erb where line #3 raised:
nil can't be converted to a Time value
<small>Submitted <%= time_ago_in_words(comment.created_at) %> ago</small>
在我提交新评论后。任何有关此错误的帮助表示赞赏或与我的主要目标有关,我已经围绕这个问题盘旋了很长时间。谢谢!
【问题讨论】:
-
您能否将错误的日志输出转储以使错误更清晰?
-
请添加您的后期控制器
-
@KartikeyTanna 代码已更新!
标签: ruby-on-rails