Ruby on rails 未定义的方法`最大人数';零级:零级
我有一个签入,提交后,它说“未定义的方法Ruby on rails 未定义的方法`最大人数';零级:零级,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有一个签入,提交后,它说“未定义的方法max_-peoplefor nil:NilClass” 签入。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
max_-people
for nil:NilClass”
签入。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
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
类签入{order created_at::desc}
计算价格
self.total\u price=self.total\u price-
self.total_price*self.room.折扣/100
结束
私有的
def check_num_guest
如果self.num\u guest>self.room.max\u people
errors.add:checkin,I18n.t(“errors.num_-people”)
结束
结束
结束
签入控制器。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
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
class CheckinsController
餐桌室:
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
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
class CreateRooms
您的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
非常感谢——托比亚斯。我明白。@AnhKieu good:)如果你把答案标为正确的话,那就太酷了:)我标了,但系统说了些什么,我不能标(((.