简体   繁体   中英

"uninitialized constant Api::V1::ReservationOptionsController::ReservationOptions"

I'm trying to create an API, but I get the error

"type": "NameError",
"message": "uninitialized constant Api::V1::ReservationOptionsController::ReservationOptions",

I cannot seem to find the issue here.

Code

routes

namespace :api, defaults: { format: :json } do
    namespace :v1 do
      resources :reservation_options, only: [:show, :create]
    end
end

controllers/api/v1/reservation_options_controller.rb

class Api::V1::ReservationOptionsController < Api::V1::BaseController
  acts_as_token_authentication_handler_for User, only: [:create]

  def show
    @reservation_option = ReservationOption.find(params[:id])
    @reservation = @reservation_option.reservation
    authorize @reservation_option
  end

  def create
    @user = current_user
    @reservation_option = ReservationOptions.new(reservation_option_params)
    authorize @reservation_option
    if @reservation_option.save
      render :show, status: :created
    else
      render_error
    end
  end

  private
  def reservation_option_params
    params.require(:reservation_option).permit(:option_id, :option_quantity, :reservation_id)
  end
end

You have an error in the action create

def create
    @user = current_user
    # change this line
    # @reservation_option = ReservationOptions.new(reservation_option_params)
    @reservation_option = ReservationOption.new(reservation_option_params)
    authorize @reservation_option
    if @reservation_option.save
      render :show, status: :created
    else
      render_error
    end
  end


It looks like ReservationOptions hasn't been defined anywhere, and you're using it in controllers/api/v1/reservation_options_controller.rb .

Make sure you've spelled it right, or that you have the appropriate model in app/models/reservation_option.rb . My guess is that it should be ReservationOption , since Rails model class names are typically singular.

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