gpt4 book ai didi

ruby-on-rails - Rspec:2 级嵌套资源的 Controller 规范

转载 作者:行者123 更新时间:2023-12-03 11:34:52 27 4
gpt4 key购买 nike

我的路线.rb

  namespace :magazine do
resources :pages do
resources :articles do
resources :comments
end
end
end

在为评论编写 Controller 规范时:
describe "GET 'index'" do
before(:each) do
@user = FactoryGirl.create(:user)
@page = FactoryGirl.build(:page)
@page.creator = @user
@page.save
@article = FactoryGirl.create(:article)
@comment_attributes = FactoryGirl.attributes_for(:comment, :article_id => @article )
end
it "populates an array of materials" do
get :index, ??
#response.should be_success
assigns(:comments)
end

it "renders the :index view" do
get :index, ??
response.should render_template("index")
end

end

知道如何给页面和文章引用以获取 :index ??
如果我给: get :index, :article_id => @article.id
我得到的错误如下:
 Failure/Error: get :index, :article_id => @article.id
ActionController::RoutingError:
No route matches {:article_id =>"3", :controller=>"magazine/comments"}

最佳答案

您的路由至少需要两个 ID:评论的父文章和文章的父页面。

namespace :magazine do
resources :pages do
resources :articles do
resources :comments
end
end
end

# => /magazine/pages/:page_id/articles/:article_id/comments

所有父 ID 必须提供此路线才能工作:
it "renders the :index view" do
get :index, {:page_id => @page.id, :article_id => @article.id}
# [UPDATE] As of Rails 5, this becomes:
# get :index, params: {:page_id => @page.id, :article_id => @article.id}
response.should render_template("index")
end

关于ruby-on-rails - Rspec:2 级嵌套资源的 Controller 规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16295721/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com