2013-04-30 32 views
17

routes.rb tôiRSpec: thông số kỹ thuật điều khiển cho nguồn lực lồng 2 mức

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

Trong khi viết thông số kỹ thuật điều khiển cho Bình luận:

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 

Bất kỳ ý tưởng làm thế nào để cung cấp cho các trang và bài viết tham khảo để có được: mục lục ?? nếu tôi cung cấp: nhận được: chỉ số,: article_id => @ article.id
Lỗi tôi nhận được là dưới đây:

Failure/Error: get :index, :article_id => @article.id 
ActionController::RoutingError: 
    No route matches {:article_id =>"3", :controller=>"magazine/comments"} 

Trả lời

33

Tuyến đường của bạn đòi hỏi phải có ít nhất hai ID: Bài viết được phụ huynh của bình luận, và trang mẹ của bài viết .

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

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

Tất cả cha mẹ ID phải được cung cấp cho tuyến đường này để làm việc:

it "renders the :index view" do 
    get :index, {:page_id => @page.id, :article_id => @article.id} 
    response.should render_template("index") 
end 
+1

đó làm việc nhờ :) – Oatmeal

+0

Vì vậy, nếu tôi muốn thử nghiệm một tình huống tiêu cực - page_id không có trong yêu cầu - sau đó làm thế nào để tôi làm điều đó? –

+0

Điều đó sẽ gây ra lỗi định tuyến. Hướng dẫn đường ray cho biết "tài nguyên nên [* không bao giờ * được lồng sâu hơn 1 cấp độ sâu] (http://guides.rubyonrails.org/routing.html#nested-resources)." – Substantial

Các vấn đề liên quan