0

我正在按照本教程为我的 rails 应用程序实现一个预订系统。我目前被卡住了,因为当我点击“新预订”按钮时,我收到了这个错误

undefined method `amoeba' for #<Class:0x00007fa37f7862a0>

![BookingsController#create 中的 NoMethodError] 1

我在我的 gem 文件中包含了 gem 'amoeba',但它仍然不起作用。谁知道怎么修它?将不胜感激。

日程安排.rb

class Schedule < ApplicationRecord

  # Tenant Of
  belongs_to :account, :inverse_of => :schedules
  accepts_nested_attributes_for :account

  belongs_to :practitioner, :inverse_of => :schedules
  accepts_nested_attributes_for :practitioner

  has_many :bookings, :inverse_of => :schedule
  accepts_nested_attributes_for :bookings

  validates :start, uniqueness: { scope: :practitioner_id, message: "You have already made this time available" }

  amoeba do
    enable
    exclude_associations :bookings
  end

end

bookings_controller.rb

class BookingsController < ApplicationController
  before_action :set_booking, only: [:show, :edit, :update, :destroy]

  # GET /bookings
  # GET /bookings.json
  def index
    @bookings = Booking.all
  end

  # GET /bookings/1
  # GET /bookings/1.json
  def show
  end

  # GET /bookings/new
  def new
    @booking = Booking.new
  end

  # GET /bookings/1/edit
  def edit
  end

  # POST /bookings
  # POST /bookings.json
  def create
    @booking = Booking.new(booking_params)

    respond_to do |format|
      if @booking.save
        format.html { redirect_to @booking, notice: 'Booking was successfully created.' }
        format.json { render :show, status: :created, location: @booking }
      else
        format.html { render :new }
        format.json { render json: @booking.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /bookings/1
  # PATCH/PUT /bookings/1.json
  def update
    respond_to do |format|
      if @booking.update(booking_params)
        format.html { redirect_to @booking, notice: 'Booking was successfully updated.' }
        format.json { render :show, status: :ok, location: @booking }
      else
        format.html { render :edit }
        format.json { render json: @booking.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /bookings/1
  # DELETE /bookings/1.json
  def destroy
    @booking.destroy
    respond_to do |format|
      format.html { redirect_to bookings_url, notice: 'Booking was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_booking
      @booking = Booking.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def booking_params
      params.require(:booking).permit(:status, :title, :cost, :start, :cancellation_reason, :refunded, :practitioner_id, :schedule_id, :lesson_id, :account_id)
    end
end

日志

Started POST "/bookings" for ::1 at 2020-03-23 12:42:06 +0100
Processing by BookingsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"IE78VI28UqOkuhXUnyY5bdvsN1S4wHw38Uu5BTZ+7ZdT0+6Ii50EThTELTiUWHuQOsOjy+MO4Dw6HyOwxJwWEw==", "booking"=>{"status"=>"Testing", "title"=>"Testing title", "cost"=>"3", "start(1i)"=>"2020", "start(2i)"=>"3", "start(3i)"=>"23", "start(4i)"=>"11", "start(5i)"=>"39", "cancellation_reason"=>"", "refunded"=>"0", "practitioner_id"=>"2", "schedule_id"=>"-1", "lesson_id"=>"2", "account_id"=>"1"}, "commit"=>"Create Booking"}
  User Load (1.4ms)  SELECT  `users`.* FROM `users` WHERE `users`.`id` = 104 ORDER BY `users`.`id` ASC LIMIT 1
  ↳ /Users/kaspervalentin/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98
   (0.3ms)  BEGIN
  ↳ app/controllers/bookings_controller.rb:30
  Account Load (0.4ms)  SELECT  `accounts`.* FROM `accounts` WHERE `accounts`.`id` = 1 LIMIT 1
  ↳ app/controllers/bookings_controller.rb:30
  Lesson Load (0.4ms)  SELECT  `lessons`.* FROM `lessons` WHERE `lessons`.`id` = 2 LIMIT 1
  ↳ app/controllers/bookings_controller.rb:30
   (0.3ms)  ROLLBACK
  ↳ app/controllers/bookings_controller.rb:30
Completed 500 Internal Server Error in 63ms (ActiveRecord: 2.8ms)



NoMethodError (undefined method `amoeba' for #<Class:0x00007fa37f7862a0>):

app/models/schedule.rb:15:in `<class:Schedule>'
app/models/schedule.rb:1:in `<main>'
app/controllers/bookings_controller.rb:30:in `block in create'
app/controllers/bookings_controller.rb:29:in `create'
4

1 回答 1

0

首先,ActiveRecord::Base当您在模型中使用变形虫时,尝试继承您的模型类,您可以避免在块enable中调用的方法,因为如果您使用类似或amoeba doenableinclude_associationexclude_association

于 2020-03-23T12:49:19.453 回答