gpt4 book ai didi

ruby-on-rails - ActionController::UrlGenerationError,没有路由匹配

转载 作者:行者123 更新时间:2023-12-03 13:29:56 25 4
gpt4 key购买 nike

我已经阅读了所有可以找到的类似问题,但仍然无法弄清我的问题。

# 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错误:
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在现有列表中成功创建一个新项目,它告诉我路由正确,我在测试中一定做错了什么。
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中的其余测试可以顺利通过。

我是否缺少明显的东西?

最佳答案

这是我必须对测试进行的修复(items_controller_spec.rb)。我没有将正确的哈希传递给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"

关于ruby-on-rails - ActionController::UrlGenerationError,没有路由匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32320214/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com