我知道这是一个非常基本的解决方案,但我现在还没有看到它。我在CommentController #create中收到“找不到没有ID的帖子”错误。
我在帖子下面创建了一个“新评论”按钮,然后该按钮会重定向到评论表单。一旦用户输入他们的评论并点击“创建评论”按钮,评论就应该显示在原始帖子下面。先感谢您。
评论控制器
Class CommentsController < ApplicationController
before_filter :authenticate_user!
def new
@comment = Comment.new
end
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:commenter, :body))
respond_to do |format|
if @comment.save
format.html { redirect_to @post, notice: 'Comment was successfully created.' }
format.json { render json: @comment, status: :created, location: @comment }
else
format.html { render action: "new" }
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
def show
@comment = Comment.new
end
end
发表/显示
<div class="row">
<div class="col-md-offset-4 col-med-8">
<div class="panel panel-default">
<div class="panel-heading center">
<% if @post.image.url %>
<%= image_tag @post.image.url(:medium) %>
<% elsif @post.video.url %>
<%= video_tag @post.video.url(:medium), controls: true, type: "video/mp4" %>
<% end %></br>
<p>
<strong>Likes:</strong>
<%= @post.get_likes.size%>
</p>
<%=link_to image_tag('like.jpg', :border => 0), likes_post_path(@post) %>
<%=link_to image_tag('unlike.jpg', :border => 0), dislikes_post_path(@post) %>
</div>
<div class="panel-body">
<p><%= @post.description %></p>
<% user = @post.user %>
<p><strong><%= link_to(user.name, user_path(user)) if @post.user %></strong></p>
<%= link_to 'New Comment', new_comment_path, class: "btn btn-danger btn-sm active" %></br>
<br><% if @post.user == current_user %>
<%= link_to edit_post_path(@post) do %>
<span class="glyphicon glyphicon-edit"></span>
Edit
<% end %>
<% end %>
<%= link_to 'Back', posts_path %>
</div>
</div>
</div>
评论/ _form
<%= form_for [@post, Comment.new] do |f| %>
<% if @comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% @comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :body %><br>
<%= f.text_area :body %>
</div>
<div class="actions">
<%= f.submit class: "btn btn-danger btn-sm" %>
</div>
<% end %>
路线
resources :comments
resources :posts do
resources :comments
end
评论/显示
<p id="notice"><%= notice %></p>
<p>
<strong>Post:</strong>
<%= @comment.post_id %>
</p>
<p>
<strong>Body:</strong>
<%= @comment.body %>
</p>
<%= link_to 'Edit', edit_comment_path(@comment) %> |
<%= link_to 'Back', comments_path %>
Rake Routes
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
答案 0 :(得分:4)
我建议进行以下更改:
<强>路线:强>
删除第一个resources :comments
。保留以下内容:
resources :posts do
resources :comments
end
帖子/显示视图
您应该使用new_post_comment_path
代替new_comment_path
。运行rake routes
查看原因。
<强> CommentsController#新强>
在@post
操作中定义new
:
def new
@post = Post.find(params[:post_id])
@comment = Comment.new
end
最后,在评论/ _form:
中将<%= form_for [@post, Comment.new] do |f| %>
更改为<%= form_for [@post, @comment] do |f| %>
。虽然我相信<%= form_for @comment do |f| %>
应该有用。
我建议您浏览Rails Guides以获取更多信息和解释。
答案 1 :(得分:0)
我相信在评论控制器的def new
中,您还需要设置@post
,而不仅仅是@comment