简体   繁体   中英

Integration test get ActionController::UrlGenerationError in rails 4

Im new to rails : ( Trying to write some test for editing the user's booking info, here are what i get,i have checked on other posts on stackover , it seems unrelated Any clue ? Your help will be greatly appreciated !

ActionController::UrlGenerationError: 

No route matches {:action=>"edit", :controller=>"bookings", :id=>nil} missing required keys: [:id]

code for testing:

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

routes: resources :bookings, only: [:show,:new, :create, :edit, :update,:destroy]

booking model : belongs_to :user

user model: 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

You are saying

edit_booking_path(@booking)

But @booking hasn't been defined and the path helper can't generate the url for a booking that doesn't exist.

You have to create an instance of Booking before you attempt to use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM