集成测试在 rails 4 中获得 ActionController::UrlGenerationError
Integration test get ActionController::UrlGenerationError in rails 4
我是 rails 的新手:(
尝试编写一些测试来编辑用户的预订信息,这是我得到的,我检查了 stackover 上的其他帖子,似乎无关任何线索?对你的帮助表示感谢 !
ActionController::UrlGenerationError:
No route matches {:action=>"edit", :controller=>"bookings", :id=>nil} missing required keys: [:id]
测试代码:
require 'test_helper'
class BookingEditTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "unsucessful edit" do
log_in_as(@user)
get edit_booking_path(@booking)
assert_template 'bookings/edit'
patch booking_path(@booking), booking: {date_of_tour: "2017-05-06", hotel_name: " ", hotel_address:"dadsada das",phone_number:12345678901 , number_of_pax:34 , pick_up_time: "9:00"}
assert_template 'bookings/edit'
end
end
路线:resources :bookings, only: [:show,:new, :create, :edit, :update,:destroy]
预订模型:belongs_to:用户
用户模型:has_many :bookings, dependent: :destroy
booking_controller.rb
class BookingsController < ApplicationController
before_action :logged_in_user, only: [:show, :edit,:create, :destroy]
def show
@booking = Booking.find(params[:id])
end
def new
@booking = Booking.new
end
def create
@booking = current_user.bookings.build(booking_params)
if @booking.save
flash[:success] = "You have submited the information successfully!"
redirect_to root_url
else
render 'new'
end
end
def edit
@booking = Booking.find(params[:id])
end
def update
@booking = current_user.bookings.find_by(params[:id])
if @booking.update_attributes(booking_params)
flash[:success] = "information updated"
redirect_to @booking
else
render 'edit'
end
end
def destroy
end
private
def booking_params
params.require(:booking).permit(:date_of_tour,:hotel_address,:hotel_name,:phone_number,:number_of_pax,:pick_up_time)
end
end
你说的是
edit_booking_path(@booking)
但@booking 尚未定义,路径助手无法为不存在的预订生成 url。
您必须先创建一个 Booking 实例,然后才能尝试使用它。
我是 rails 的新手:( 尝试编写一些测试来编辑用户的预订信息,这是我得到的,我检查了 stackover 上的其他帖子,似乎无关任何线索?对你的帮助表示感谢 !
ActionController::UrlGenerationError:
No route matches {:action=>"edit", :controller=>"bookings", :id=>nil} missing required keys: [:id]
测试代码:
require 'test_helper'
class BookingEditTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
test "unsucessful edit" do
log_in_as(@user)
get edit_booking_path(@booking)
assert_template 'bookings/edit'
patch booking_path(@booking), booking: {date_of_tour: "2017-05-06", hotel_name: " ", hotel_address:"dadsada das",phone_number:12345678901 , number_of_pax:34 , pick_up_time: "9:00"}
assert_template 'bookings/edit'
end
end
路线:resources :bookings, only: [:show,:new, :create, :edit, :update,:destroy]
预订模型:belongs_to:用户
用户模型:has_many :bookings, dependent: :destroy
booking_controller.rb
class BookingsController < ApplicationController
before_action :logged_in_user, only: [:show, :edit,:create, :destroy]
def show
@booking = Booking.find(params[:id])
end
def new
@booking = Booking.new
end
def create
@booking = current_user.bookings.build(booking_params)
if @booking.save
flash[:success] = "You have submited the information successfully!"
redirect_to root_url
else
render 'new'
end
end
def edit
@booking = Booking.find(params[:id])
end
def update
@booking = current_user.bookings.find_by(params[:id])
if @booking.update_attributes(booking_params)
flash[:success] = "information updated"
redirect_to @booking
else
render 'edit'
end
end
def destroy
end
private
def booking_params
params.require(:booking).permit(:date_of_tour,:hotel_address,:hotel_name,:phone_number,:number_of_pax,:pick_up_time)
end
end
你说的是
edit_booking_path(@booking)
但@booking 尚未定义,路径助手无法为不存在的预订生成 url。
您必须先创建一个 Booking 实例,然后才能尝试使用它。