Ruby on rails 对我的reddit克隆的评论ruby出现名称错误
我正在尝试为我的Reddit克隆添加注释功能。这是创建注释并将其添加到帖子的注释控制器Ruby on rails 对我的reddit克隆的评论ruby出现名称错误,ruby-on-rails,comments,Ruby On Rails,Comments,我正在尝试为我的Reddit克隆添加注释功能。这是创建注释并将其添加到帖子的注释控制器 class CommentsController < ApplicationController def new @topic = Topic.find(params[:topic_id]) @post = Post.find(params[:id]) @comment = Comment.new #authorize @comme
class CommentsController < ApplicationController
def new
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
@comment = Comment.new
#authorize @comment # from include Pundit in the application controller, authorize is an inherited method
end
def create
@topic = Topic.find(params[:topic_id])
@post = Post.find(params[:id])
@comment = current_user.comments.build(comment_params)
end
private
def comment_params
params.require(:comment).permit(:text)
end
end
顺便问一下:这个routes.rb文件看起来怎么样
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index]
end
resources :posts do
resources :comments, only: [:create]
end
end
您似乎正在将
注释作为文本
发送到部分
... locals: { topic: @topic, post: @post, text: @post.comments.new } %>
^^^^
顺便说一下,您没有在创建操作中保存注释。尝试:
<%= render partial: 'comments/form', locals: { topic: @topic, post: @post, comment: @post.comments.new } %>
...
<%= form_for [post, comment] do |f| %>
<%= f.label :text %>
<%= f.text_area :text, rows: 10, class: 'form-control', placeholder: "Enter your comment" %>
<%= f.submit "Save", class: 'btn btn-success' %>
<% end %>
...
...
...
如果没有帮助,请尝试临时注释表单\u group\u标记并在此处发送错误Ok cool,我想我必须将注释的文本传递给分部。还有一个命名错误:部分感谢中的文本对我来说没有意义。@ClydeBrown将comment
变量包含在helper的表单中(form\u for[topic,post,comment]
)。感谢添加的注释,我的问题是什么?@ClydeBrown查看生成的表单URL。如果没有注释,URL将指向帖子。还要确保资源:注释嵌套在routes中的posts下。上面的routes.rb看起来如何?我在注释和不注释时都得到了一个未定义的方法“topic_post_comments_path”。我编辑了我的问题以包括我的rake routes
和我的routes.rbI编辑了我的答案-try:text正在拉一个nomethoder错误也可能是注释ControllerIt在模式中被调用的body有问题,所以我应该称它为:body
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:update]
resources :topics do
resources :posts, except: [:index]
end
resources :posts do
resources :comments, only: [:create]
end
end
... locals: { topic: @topic, post: @post, text: @post.comments.new } %>
^^^^
<%= render partial: 'comments/form', locals: { topic: @topic, post: @post, comment: @post.comments.new } %>
...
<%= form_for [post, comment] do |f| %>
<%= f.label :text %>
<%= f.text_area :text, rows: 10, class: 'form-control', placeholder: "Enter your comment" %>
<%= f.submit "Save", class: 'btn btn-success' %>
<% end %>
...