【问题标题】:RSpec no route matches - missing required keys: [:id]RSpec 没有路由匹配 - 缺少必需的键:[:id]
【发布时间】:2017-07-22 16:36:24
【问题描述】:

我的规范由于没有路由匹配而失败,这里是相关代码:

describe AdminController do
  before(:each) do
    @request.env["devise.mapping"] = Devise.mappings[:user]
    @admin = create(:admin)
    @user = create(:user)
    sign_in @admin, scope: :user
  end

  context "with admin user" do
    describe "DELETE #destroy" do
      it "deletes the contact" do
        puts @user.inspect
        expect{
          delete admin_destroy_user_path, id: @user.id
        }.to change(Profile, :count).by(-1)
      end

错误:

 AdminController with admin user DELETE #destroy deletes the contact
   Failure/Error: delete admin_destroy_user_path, id: @user.id

 ActionController::UrlGenerationError:
   No route matches {:action=>"destroy", :controller=>"admin"} missing    required keys: [:id]

路线:

admin_destroy_user DELETE /admin/:id(.:format)                              admin#destroy

我不确定我错过了什么,任何帮助将不胜感激!

【问题讨论】:

  • 您使用的是哪个版本的 RSpec?您可能需要像这样传递 id:delete admin_destroy_user_path, params: { id: @user.id } 假设 @user.id 实际上返回一个值。
  • 或者你可以使用admin_destroy_user_path(@user.id)
  • 谢谢先生们,但是这些都不起作用,我使用 params: 选项得到了同样的错误。如果我使用 Igor 的选项,我会得到 "ActionController::UrlGenerationError: No route matches {:action=>"/admin/2", :controller=>"admin"}" 另外我使用的是 RSpec 3.5.4 版。
  • 你试过了吗:admin_destroy_user_path(id: @user.id)
  • 感谢 Taryn,我通过安装 Rails Admin 解决了这个问题。现在我可以做我需要的了。

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


【解决方案1】:

感谢大家的帮助,我最终重构为使用 Rails Admin。

【讨论】:

    【解决方案2】:

    无论如何,您应该使用括号将 id 传递给路由:

    delete admin_destroy_user_path(id: @user.id)
    

    【讨论】:

      猜你喜欢
      • 2014-03-09
      • 1970-01-01
      • 2021-11-18
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多