如何路由到深度嵌套记录的新操作?
我有一个场地、评论和评论表,其中一个场地有很多评论,每个评论都有很多评论。
目前,评论在场地 show.html.erb 上以部分形式显示,每个部分的底部都有一个“添加评论”链接。
如何获取该链接以路由到新的评论操作?
到目前为止我的代码如下:
routes
Go::Application.routes.draw do
resources :venues do
resources :reviews
end
resources :reviews do
resources :comments
end
end
commentscontroller
class CommentsController < ApplicationController
def new
@review = Review.find(params[:review_id])
@comment = @review.comments.build
end
def create
@review = Review.find(params[:review_id])
@comment = current_user.comments.create!(params[:comment])
@comment.review = @review
if @comment.save
flash[:notice] = 'Comment added'
redirect_to comments_path
else
render :action => :new
end
end
end
_review.html.erb
<div class="review">
<div class="review_content">
<h2 class="review_partial_title"><%= review.title %></h2>
<p class="review_body"><%= review.body %></p>
</div>
<div class="clearall"></div>
<div class="review_options">
<div class="review_partial_option">
<%= link_to 'add comment', review_comments_path(review) %>
</div>
</div>
</div>
<%= link_to 'add comment', review_comments_path( review) %>
将我带到评论索引页面 (/review/168/comments),并且仅显示为该特定评论撰写的评论。
我认为使用
<%= link_to 'add comment', new_review_comments_path(review) %>
会起作用,但它给了我一个 NoMethodError in Venues#show undefined method `new_review_comments_path' for #<#:0x5878e18>错误。
感谢您的帮助,非常感谢!
I have a table of venues, reviews and comments, where a venue has many reviews and each review has many comments.
Currently the reviews are being shown as partials on the venues show.html.erb with a 'add comment' link at the bottom of each partial.
How do I get that link to route to the new comment action?
Heres my code so far:
routes
Go::Application.routes.draw do
resources :venues do
resources :reviews
end
resources :reviews do
resources :comments
end
end
comments controller
class CommentsController < ApplicationController
def new
@review = Review.find(params[:review_id])
@comment = @review.comments.build
end
def create
@review = Review.find(params[:review_id])
@comment = current_user.comments.create!(params[:comment])
@comment.review = @review
if @comment.save
flash[:notice] = 'Comment added'
redirect_to comments_path
else
render :action => :new
end
end
end
_review.html.erb
<div class="review">
<div class="review_content">
<h2 class="review_partial_title"><%= review.title %></h2>
<p class="review_body"><%= review.body %></p>
</div>
<div class="clearall"></div>
<div class="review_options">
<div class="review_partial_option">
<%= link_to 'add comment', review_comments_path(review) %>
</div>
</div>
</div>
<%= link_to 'add comment', review_comments_path(review) %>
takes me to the comments index page (/review/168/comments) and only displays the comments written for that particular review.
I thought using
<%= link_to 'add comment', new_review_comments_path(review) %>
would work but its giving me a NoMethodError in Venues#show undefined method `new_review_comments_path' for #<#:0x5878e18> error.
Thanks for any help its much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加新评论的链接应该是:
The link to add a new comment should be: