MY RAILS PROJECT :: HOTELIFY
As a part of phase 3, we were tasked with creating a web application using the Ruby on Rails framework. The Rails project is combination of our several weeks of learning different approaches to understand the MVC, Restful routes, SQL queries, Active Record Associations and many more. These fundamentals were the key points in moving forward with the new attempt in creating the Ruby on Rails project.
I started my project by installing the rails gem and the most important part was running rails new project-name where project-name can be written anything that describes our project. This line of code would build a rails application.Rails has made our life easier because, it has codes that build models, views, controllers, routes for us. So, I created mine by using the following line of code:
rails g resource Hotel name price:float free_wifi:string free_breakfast:string
This magically (not actually magically) creates:
# a migration in db/migrate/20210118041252_create_hotels
class CreateHotels < ActiveRecord::Migration[5.2]
create_table :hotels do |t|
end# a Hotel model in app/models/hotel.rbclass Hotel < ApplicationRecord
end# a Hotel controller in app/controllers/hotels_controller.rbclass HotelsController < ApplicationController
end# opens up all of the routes in config/routes.rbRails.application.routes.draw do
For my project, I also used Active Record Validations so that only valid data is saved into our database. We can also make a custom validation. For my hotel model, I also made a custom validation which is as shown below:
For custom validation, we just write a singular form of validates.
validate :dublicate_attributes, on: :create
Here, the last part (on: :create) was a solution to my problem where I wanted to update the data and was not able to enter the same hotel name. So, this validation is run only when a new hotel is added.
Another cool stuff that I added (also one of the requirement) in my project was scope and class method.
Both of these could be written in exchangeable way. These are just methods.
Talking about my project, Hotelify is a hotel rating web application. We can create hotels and rate them. We can filter the hotels by the highest ratings, the highest to lowest price and viceversa. We can edit or delete the hotels/ratings that we created. If you want to have a look to my project demo, here it is( also forgive my poor styling as I am a CSS beginner):