有一个如下所示的routes.rb文件:
resources :posts do
resources :comments, shallow: true
end
尝试正确测试_comment部分的渲染,这是我的规范:
require 'spec_helper'
describe 'rendering the comment partial' do
let!(:comment) { create(:comment, name: "Richard Stokes")}
it "displays the comment's username" do
render partial: 'comments/comment.html.haml', locals: { comment: comment }
expect(rendered).to have_content("Richard Stokes")
end
end
但是当我运行它时,我收到以下错误:
Failure/Error: render partial: 'comments/comment.html.haml', locals: { comment: comment }
ActionView::Template::Error:
undefined method `comments_path' for #<#<Class:0x0000000669f0b8>:0x000000067249e8>
在我的路由中添加一个非嵌套的resources :comments
可以解决问题,但我希望能够保留原来的路由,而只是通过正确的路径将注释部分呈现。我该怎么做呢?