如何路由到深度嵌套记录的新操作?

发布于 2024-11-26 07:07:51 字数 1801 浏览 0 评论 0原文

我有一个场地、评论和评论表,其中一个场地有很多评论,每个评论都有很多评论。

目前,评论在场地 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两相知 2024-12-03 07:07:51

添加新评论的链接应该是:

<%= link_to 'add comment', new_review_comment_path(review) %>

The link to add a new comment should be:

<%= link_to 'add comment', new_review_comment_path(review) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文