0

嗨,我正在实施一个项目。在这个 Loan_fines 表中引用了两个表,即书籍和类别。

我收到以下错误:-

NoMethodError in LoanFinesController#create

undefined method `blank' for [1, 1]:Array

app/models/loan_fine.rb:32:in `book_or_category'
app/controllers/loan_fines_controller.rb:46:in `block in create'
app/controllers/loan_fines_controller.rb:45:in `create'

我的贷款罚款表如下:-

  id integer NOT NULL DEFAULT nextval('loans_fines_id_seq'::regclass),
  category_id integer,
  book_id integer,
  loan_duration integer,
  fine_amount numeric(8,2) DEFAULT 0.0,
  fine_duration integer,
  deleted integer NOT NULL DEFAULT 0,
  CONSTRAINT loans_fines_pkey PRIMARY KEY (id)

这张表是我学长给的,不知道是什么意思,nextval('loans_fines_id_seq'::regclass)为什么用

我的创建贷款罚款控制器如下:-

 def create
    @loan_fine = LoanFine.new(params[:loan_fine])

    respond_to do |format|
      if @loan_fine.save
        format.html { redirect_to @loan_fine, notice: 'Loan fine was successfully created.' }
        format.json { render json: @loan_fine, status: :created, location: @loan_fine }
      else
        format.html { render action: "new" }
        format.json { render json: @loan_fine.errors, status: :unprocessable_entity }
      end
    end
  end

我的贷款罚款模型如下:-

class LoanFine < ActiveRecord::Base

    attr_accessible :book_id, :category_id, :loan_duration, :fine_amount, :fine_duration

    # ------- ASSOCIATIONS -----------

    belongs_to :book

    belongs_to :category

    # ------ VALIDATIONS ------

    validate :book_or_category

  private

    def book_or_category
        if [self.book_id, self.category_id].compact.blank.size == 0
            errors[:base] << ("Please choose either book or category.")
        end
    end

end

我被困在这个我不知道如何实现这个。谁能帮我

4

1 回答 1

0

您的错误在此文件中

app/models/loan_fine.rb

特别是堆栈跟踪中指定的第 32 行

app/models/loan_fine.rb:32:in `book_or_category'

但是根据错误,您正在调用blank而不是blank?. 因此,解决方案是更改blankblank?您的 loan_fine 模型的第 32 行。

更新:根据您的代码,您想查看book_idcategory_id是否存在。你有两个选择

  1. if self.book_id.blank? && self.category_id.blank?
  2. if [self.book_id, self.category_id].reject(&:blank?).size == 0
于 2013-03-15T11:09:25.770 回答