2017-04-23 215 views
0

我有一個籤,提交後,它說: 「未定義的方法max_people爲無:NilClass」未定義的方法`max_people」的零:NilClass

checkin.rb:

class Checkin < ApplicationRecord 
    has_one :payment, dependent: :destroy 
    has_many :service_items, dependent: :destroy 

    before_save :calculate_price 

    belongs_to :user 
    belongs_to :room 

    validates :contact_name, presence: true 
    validates :contact_phone, presence: true, numericality: true, length: {maximum: 11, minimum: 8} 
    validate :check_num_guest 

    enum status: [:waiting_pay, :paid, :accept, :reject] 

    scope :order_desc, ->{order created_at: :desc} 

    def calculate_price 
    self.total_price = self.total_price - 
    self.total_price * self.room.discount/100 
    end 

    private 
    def check_num_guest 
    if self.num_guest > self.room.max_people 
     errors.add :checkin, I18n.t("errors.num_people") 
    end 
    end 
end 

checkins_controller.rb:

class CheckinsController < ApplicationController 
    load_resource :room 

    def index 
    @checkins = Checkin.order_desc.paginate page: params[:page] 

    end 

    def create 
    @checkin = Checkin.new checkin_params 
    @checkin.status = Checkin.statuses[:waiting_pay] 
    if @checkin.save 
     @payment = Payment.new 
     flash[:success] = t "flash.checkins.checkin_success" 
     render "payments/new" 
    else 
     @supports = Support::RoomSupport.new room: @room 
     flash[:danger] = t "flash.checkins.create_checkin_fail" 
     render "rooms/show" 
    end 
    end 

    def show 
    @payment = Payment.new 
    end 

    def update 
    if @checkin.update_attributes checkin_params 
     flash[:success] = t "flash.checkins.checkin_canceled" 
    else 
     flash[:danger] = t "flash.checkins.checkin_cancel_fail" 
    end 
    redirect_to checkins_path 
    end 

    rescue_from ActiveRecord::RecordNotFound do 
    flash[:danger] = t "flash.rooms.room_not_found" 
    redirect_to root_path 
    end 

    private 
    def checkin_params 
    params.require(:checkin).permit :user_id, :num_guest, :contact_name, :contact_phone, :contact_address, :contact_email, :description, :total_price, :is_cancel, :status, :fromdate, :todate 
    end 
end 

表室:

class CreateRooms < ActiveRecord::Migration[5.0] 
    def change 
    create_table :rooms do |t| 
     t.string :number 
     t.boolean :status, default: true 
     t.float :price 
     t.string :picture 
     t.string :description 
     t.integer :max_people 
     t.string :facilities 
     t.datetime :deleted_at 
     t.integer :discount 
     t.belongs_to :room_type, foreign_key: true 

     t.timestamps 
    end 
    end 
end 

回答

1

check_num_guest驗證嘗試撥打房間關係,但你以後不要任何房間分配給籤#room返回nil

您必須設置簽入的房間。但是,無論如何,如果沒有設置空間,我會推薦您將驗證轉換回來。存在驗證器負責未設置的值。

事情是這樣的:

def check_num_guest 
    return unless self.room.present? 
    if self.num_guest > self.room.max_people 
    errors.add :checkin, I18n.t("errors.num_people") 
    end 
end 
+0

感謝這麼much-托比亞斯。我明白了。 – ktanh

+0

@AnhKieu好:)如果你把答案標記爲正確的話,會很酷:) – Tobias

+0

我標記了,但系統說了些什麼,我不能標記。 :(((。 – ktanh

相關問題