开发者

How can I route to the new action for a deeply nested record?

开发者 https://www.devze.com 2023-03-22 13:01 出处:网络
I have a table of venues, reviews and comments, where a venue has many reviews and each review has many comments.

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!


The link to add a new comment should be:

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

精彩评论

暂无评论...
验证码 换一张
取 消