<repo_name>vankhoenhadat/FirstRoR<file_sep>/app/helpers/rooms_helper.rb
module RoomsHelper
end
class RoomValidator
  include Veto.validator

  validates :name, :presence => {:message => "Tên phòng không để trống"}
  validates :minrate, :inclusion => 0..100
end
<file_sep>/test/controllers/bookingtests_controller_test.rb
require 'test_helper'

class BookingtestsControllerTest < ActionController::TestCase
  setup do
    @bookingtest = bookingtests(:one)
  end

  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:bookingtests)
  end

  test "should get new" do
    get :new
    assert_response :success
  end

  test "should create bookingtest" do
    assert_difference('Bookingtest.count') do
      post :create, bookingtest: { active: @bookingtest.active, customer_name: @bookingtest.customer_name, from_date: @bookingtest.from_date, hotel_name: @bookingtest.hotel_name, night_number: @bookingtest.night_number, to_date: @bookingtest.to_date }
    end

    assert_redirected_to bookingtest_path(assigns(:bookingtest))
  end

  test "should show bookingtest" do
    get :show, id: @bookingtest
    assert_response :success
  end

  test "should get edit" do
    get :edit, id: @bookingtest
    assert_response :success
  end

  test "should update bookingtest" do
    patch :update, id: @bookingtest, bookingtest: { active: @bookingtest.active, customer_name: @bookingtest.customer_name, from_date: @bookingtest.from_date, hotel_name: @bookingtest.hotel_name, night_number: @bookingtest.night_number, to_date: @bookingtest.to_date }
    assert_redirected_to bookingtest_path(assigns(:bookingtest))
  end

  test "should destroy bookingtest" do
    assert_difference('Bookingtest.count', -1) do
      delete :destroy, id: @bookingtest
    end

    assert_redirected_to bookingtests_path
  end
end
<file_sep>/app/models/room.rb
class Room
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, type: String
  field :descriptions, type: String
  field :minrate, type: Float
  field :active, type: Boolean

  embedded_in :Booking
end
<file_sep>/app/models/bookingtest.rb
class Bookingtest
  include Mongoid::Document
  include Mongoid::Timestamps

  field :customer_name, type: String
  field :hotel_name, type: String
  field :night_number, type: Integer
  field :from_date, type: DateTime
  field :to_date, type: DateTime
  field :active, type: Boolean

  default_scope ->{ where(active: true) }

  embeds_many :Room
end
<file_sep>/app/controllers/rooms_controller.rb
class RoomsController < ApplicationController

  before_action :set_room, only: [:show, :edit, :update, :destroy]    

  # GET /rooms
  def index
    @rooms = Room.all
  end

  # GET /rooms/1
  def show
  end

  # GET /rooms/new
  def new
    @room = Room.new
    @error =[]
    # @error.push("sdfsdf")
  end

  # GET /rooms/1/edit
  def edit
  end

  # POST /rooms
  def create
    # @room = Room.new(room_params)
    @room = Room.new
    @room.name = params[:room][:name]
    @room.descriptions = params[:room][:descriptions]
    @room.minrate = params[:room][:minrate]

    validator = RoomValidator.new
    # validator.valid?(@room)
    # validator.validate!(@room) # => # => Veto::InvalidEntity, ["name is not present", "..."]
    # validator.errors.full_messages # => ["first name is not present", "..."]

    if validator.valid?(@room)
      if @room.save
        redirect_to @room, notice: 'Room was successfully created.'
      else
        render :new
      end
    else
      @error = validator.errors.full_messages
      render :new
    end
  end

  # PATCH/PUT /rooms/1
  def update
    if @room.update(room_params)
      redirect_to @room, notice: 'Room was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /rooms/1
  def destroy
    @room.destroy
    redirect_to rooms_url, notice: 'Room was successfully destroyed.'
  end

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

    # Only allow a trusted parameter "white list" through.
    def room_params
      params.require(:room).permit(:name, :descriptions, :minrate, :active)
    end
end
<file_sep>/app/controllers/bookingtests_controller.rb
class BookingtestsController < ApplicationController
  before_action :set_bookingtest, only: [:show, :edit, :update, :destroy]

  # GET /bookingtests
  def index
    @bookingtests = Bookingtest.unscoped.page(params[:page]).per(2)
    a=1
  end

  # GET /bookingtests/1
  def show
    email= params[:email]
    password= params[:password]
    subject= params[:subject]
    issue_type= params[:issue_type]
    priority= params[:priority]
    description= params[:description]
  end

  # GET /bookingtests/new
  def new
    @bookingtest = Bookingtest.new
  end

  # GET /bookingtests/1/edit
  def edit
  end

  # POST /bookingtests
  def create
    @bookingtest = Bookingtest.new(bookingtest_params)
    @rooms = []
    room = Room.new
    room.name = "room Name";
    room.descriptions = "descriptions"
    room.minrate = 12345
    room.active  = true;
    @rooms.push(room);
    @bookingtest.Room = @rooms

    if @bookingtest.save
      redirect_to @bookingtest, notice: 'Bookingtest was successfully created.'
    else
      render :new
    end
  end

  # PATCH/PUT /bookingtests/1
  def update
    rooms = params[:bookingtest][:Room]
    booking = Bookingtest.new
    booking._id = params[:id]
    booking.customer_name = params[:bookingtest][:customer_name]
    booking.hotel_name = params[:bookingtest][:hotel_name]
    booking.night_number = params[:bookingtest][:night_number]
    booking.Room = rooms
    if @bookingtest.update(bookingtest_params)
      @bookingtest.update_attribute(:Room, rooms)
      redirect_to @bookingtest, notice: 'Bookingtest was successfully updated.'
    else
      render :edit
    end
  end

  # DELETE /bookingtests/1
  def destroy
    @bookingtest.destroy
    redirect_to bookingtests_url, notice: 'Bookingtest was successfully destroyed.'
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_bookingtest
      @bookingtest = Bookingtest.unscoped.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def bookingtest_params
      booking = params.require(:bookingtest).permit(:customer_name, :hotel_name, :night_number, :from_date, :to_date, :active)
      booking

    end
end
