ActionController::UrlGenerationError, 没有路由匹配 [英] ActionController::UrlGenerationError, No route matches

查看:40
本文介绍了ActionController::UrlGenerationError, 没有路由匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通读了我能找到的所有类似问题,但仍然无法弄清楚我的问题.

I've read through every similar question I could find and still can't figure out my problem.

# routes.rb
Rails.application.routes.draw do
  resources :lists, only: [:index, :show, :create, :update, :destroy] do
    resources :items, except: [:new]
  end
end

<小时>

# items_controller.rb (excerpt)
class ItemsController < ApplicationController
  ...

  def create
    @list = List.find(params[:list_id])
    ...
  end
  ...
end

<小时>

# items_controller_spec.rb (excerpt)
RSpec.describe ItemsController, type: :controller do
   ...

  let!(:list) { List.create(title: "New List title") }

  let(:valid_item_attributes) {
    { title: "Some Item Title", complete: false, list_id: list.id }
  }

  let!(:item) { list.items.create(valid_item_attributes) }
  describe "POST #create" do
    context "with valid params" do
      it "creates a new item" do
        expect {
          post :create, { item: valid_item_attributes, format: :json  }
        }.to change(Item, :count).by(1)
      end
    end
  end
  ...
end

还有 RSpec 错误:

And the RSpec error:

1) ItemsController POST #create with valid params creates a new item
     Failure/Error: post :create, { item: valid_item_attributes, format: :json  }
     ActionController::UrlGenerationError:
       No route matches {:action=>"create", :controller=>"items", :format=>:json, :item=>{:title=>"Some Item Title", :complete=>false, :list_id=>1}}

rake routes 的输出:

list_items     GET    /lists/:list_id/items(.:format)          items#index
               POST   /lists/:list_id/items(.:format)          items#create
edit_list_item GET    /lists/:list_id/items/:id/edit(.:format) items#edit
     list_item GET    /lists/:list_id/items/:id(.:format)      items#show
               PATCH  /lists/:list_id/items/:id(.:format)      items#update
               PUT    /lists/:list_id/items/:id(.:format)      items#update
               DELETE /lists/:list_id/items/:id(.:format)      items#destroy

我可以通过 curl 在现有列表中成功创建一个新项目,它告诉我路线没问题,我一定是在测试中做错了什么.

I can successfully create a new item in an existing list via curl which tells me that the route is ok, I must be doing something wrong in my test.

curl -i -X POST -H "Content-Type:application/json" -H "X-User-Email:admin@example.com" -H "X-Auth-xxx" -d '{ "item": { "title": "new item", "complete": "false"} }' http://localhost:3000/lists/5/items

我真的很困惑.我的路线设置正确.ItemsController#create 方法肯定存在.items_controller_spec.rb 中的其余测试均顺利通过.

I am really confused. My routes are setup correctly. A ItemsController#create method definitely exists. The rest of the tests in items_controller_spec.rb pass without issue.

我是否遗漏了一些明显的东西?

Am I missing something obvious?

推荐答案

以下是我必须对测试进行的修复 (items_controller_spec.rb).我没有将正确的哈希值传递给 post create:.

Here are the fixes I had to make to my tests (items_controller_spec.rb). I was not passing the correct hash to post create:.

  describe "POST #create" do
    context "with valid params" do
      it "creates a new item" do
        expect {
          post :create, { list_id: list.id, item: valid_item_attributes, format: :json  }
        }.to change(Item, :count).by(1)
      end

      it "assigns a newly created item as @item" do
        post :create, { list_id: list.id, item: valid_item_attributes, format: :json  }

        expect(assigns(:item)).to be_a(Item)
        expect(assigns(:item)).to be_persisted
      end
    end # "with valid params"

    context "with invalid params" do
      it "assigns a newly created but unsaved item as @item" do
        post :create, { list_id: list.id, item: invalid_item_attributes, format: :json  }

        expect(assigns(:item)).to be_a_new(Item)
      end

      it "returns unprocessable_entity status" do
        put :create, { list_id: list.id, item: invalid_item_attributes, format: :json }

        expect(response.status).to eq(422)
      end
    end # "with invalid params"
  end # "POST #create"

这篇关于ActionController::UrlGenerationError, 没有路由匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆