加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Rspec:控制器规范2级嵌套资源

发布时间:2020-12-16 20:44:11 所属栏目:百科 来源:网络整理
导读:我的路线 namespace :magazine do resources :pages do resources :articles do resources :comments end end end 在撰写控制器规范时,请注意: describe "GET 'index'" do before(:each) do @user = FactoryGirl.create(:user) @page = FactoryGirl.build(:
我的路线
namespace :magazine do
   resources :pages do
     resources :articles do
       resources :comments
     end
   end
  end

在撰写控制器规范时,请注意:

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}
  response.should render_template("index")
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读