【发布时间】:2018-07-11 15:23:35
【问题描述】:
我正在使用 ancestry gem 在我的帖子中创建嵌套评论。
我使用 Rails 5。
这是我的代码:
后模型
has_many :comments
评论模型
has_ancestry
belongs_to :post
我在视图/帖子/节目中渲染所有 cmets
<%= render 'comment' %>
内部视图/cmets/_cmets
<%= div_for(comment) do %>
<%= comment.body %>
<%= link_to "Reply", new_post_comment_path(:parent_id => comment, :post_id => @post.id) %>
<% end %>
cmets_controller
def create
@comment = @post.comments.new(comment_params)
@comment.user = current_user
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 new
@comment = Comment.new(:parent_id => params[:parent_id])
end
意见表
<%= simple_form_for new_post_comment_path(@post,@comment) do |f| %>
<%= f.hidden_field :parent_id %>
<%= f.text_area :body, class: "form-control" %>
<%= f.submit "Leave Comment", class: 'btn btn-main btn-block' %>
<% end %>
Routes.rb
resources :posts do
resources :comments
end
rake 路线
post_comments GET /:post_id/comments(.:format) comments#index
POST /:post_id/comments(.:format) comments#create
new_post_comment GET /:post_id/comments/new(.:format) comments#new
edit_post_comment GET /:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /:post_id/comments/:id(.:format) comments#show
PATCH /:post_id/comments/:id(.:format) comments#update
PUT /:post_id/comments/:id(.:format) comments#update
DELETE /:post_id/comments/:id(.:format) comments#destroy
在这个网址http://localhost:3000/post_id/comments/new?parent_id=10
,我点击“回复评论”,它呈现了新的评论表单。
但是如果我点击提交按钮,我得到了这个错误
没有路线匹配 [POST] "/post_id/cmets/new"
请有人帮我解决它。谢谢你
【问题讨论】:
-
你错过了一条路线,当你运行
rake routes时没有POST /:post_id/comments/new(.:format) comments#new。可以发routes.rb -
这是我的路线:资源 :posts 做资源 :cmets 结束