将2个对象传递到RSpec中的命名路由

时间:2018-11-28 13:21:52

标签: ruby-on-rails rspec routes rspec-rails

在我的RSpec测试中,我这样做: delete recurring_events_path(@group, @recurring_event),但产生DELETE "/groups/705777939/recurring_events.496

我应该如何设计recurring_events_path的参数,以便产生/groups/705777939/recurring_events/496

routes.rb

 45   resources :groups, except: %i[new edit]
 [snipp..]
 56   scope "groups/:group_id" do
 57     resources :posts, except: %i[new edit]
 58     put "posts/:id/pin", to: "posts#pin"
 59
 60     resources :recurring_events, except: %i[show]
 61     get "recurring_events/upcoming" => "recurring_events#upcoming", as: :upcoming
 62     get "recurring_events/past" => "recurring_events#past", as: :past
 63
 64     scope "/posts/:post_id" do
 65       resources :comments, except: %i[new edit]
 66     end
 67   end

$耙路

recurring_events GET    /groups/:group_id/recurring_events(.:format)            recurring_events#index
                 POST   /groups/:group_id/recurring_events(.:format)            recurring_events#create
                 PATCH  /groups/:group_id/recurring_events/:id(.:format)        recurring_events#update
                 PUT    /groups/:group_id/recurring_events/:id(.:format)        recurring_events#update
                 DELETE /groups/:group_id/recurring_events/:id(.:format)        recurring_events#destroy

3 个答案:

答案 0 :(得分:0)

recurring_events_path代表您的#index路线

如果您在路由中使用resource,则路径应为destroy_recurring_events_path。否则,您需要在路由声明中指定as:选项。像as: :destroy_recurring_events

您可以使用rake routes命令查看路由的别名

答案 1 :(得分:0)

更改路由以使用嵌套资源,如下所示:

 resources :groups, except: %i[new edit]
   resources :posts, except: %i[new edit]
     resources :comments, except: %i[new edit] #be careful with this, tree levels of nesting is not recommended, I would move this out of the "group" namespace
     member do
       put :pin
     end
   end

   resources :recurring_events, except: %i[show] do
     collection do
       get :upcoming
       get :past
     end
   end
 end

现在rake routes应该会给您所有带有其名称的路线。

有关文档的更多信息:https://guides.rubyonrails.org/routing.html#nested-resources

答案 2 :(得分:0)

您只需要将recurring_event_path用作单个事件,而不是recurring_events_path