gpt4 book ai didi

ruby-on-rails - ActiveRecord::RecordNotFound:

转载 作者:行者123 更新时间:2023-11-28 21:33:16 27 4
gpt4 key购买 nike

我用 rspec 和下一个简单代码测试我的应用程序:

require 'spec_helper'

describe CarsController do
describe "GET 'new'" do
it "should be successful" do
visit new_user_car_path(:user_id=>"28")
response.should be_success
end
end
end

当我运行它时,我收到了这条消息

Failure/Error: visit new_user_car_path(:user_id=>"28")
ActiveRecord::RecordNotFound:
Couldn't find User with id=28
# ./app/controllers/cars_controller.rb:3:in `new'
# ./spec/controllers/tanking_logs_controller_spec.rb:6:in `block (3 levels) in <top (required)>'

我不知道这是怎么回事,在我的 route 出现了 new_user_car_path,看...

user_cars GET    /users/:user_id/cars(.:format)                               cars#index
POST /users/:user_id/cars(.:format) cars#create
new_user_car GET /users/:user_id/cars/new(.:format) cars#new
edit_user_car GET /users/:user_id/cars/:id/edit(.:format) cars#edit
user_car GET /users/:user_id/cars/:id(.:format) cars#show
PUT /users/:user_id/cars/:id(.:format) cars#update
DELETE /users/:user_id/cars/:id(.:format) cars#destroy

如果需要,这是我的 routes.rb

Estaciones::Application.routes.draw do
root :to => "static_pages#home"
match '/contact', :to=>'static_pages#contact'
match '/about', :to=>'static_pages#about'
devise_for :users
resources :gas_stations
resources :users do
resources :cars do
#resources :tanking_logs
end
end
...

最佳答案

new_user_car GET    /users/:user_id/cars/new(.:format)

您的路径需要一个 :user_id 参数才能工作,而您没有提供。

编辑 根据您的评论,如果您使用的是 Factory Girl Rails您可以设置工厂来创建用户,然后执行以下操作:

user = Factory.create :user
visit new_user_car_path(:id => user.id)

否则,您可以手动创建它们:

user = User.create!(:name => "Joe", ...)
visit new_user_car_path(:id => user.id)

关于ruby-on-rails - ActiveRecord::RecordNotFound:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11728879/

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