Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 1, 2022 02:52 pm GMT

Steak Whizard

The Philly Cheesesteak. Whether you love it or hate it, you likely have heard the never-ending debate about which is the best.

Enter Steak Whizard - a web application solely dedicated to finding the best steak in Philly. Review, rate, and let's settle the debate.

The application is built with a React frontend (and the much appreciated help of the Bootstrap library) and a Ruby on Rails back end with a PostgreSQL database. It is hosted on Heroku.

There are three distinct Rails models that are mapped to tables in the database - User, Steak, Review. The Review table is the join table, belonging to one instance of User and one instance of Steak:

class Review < ApplicationRecord    validates :rating, presence: true, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 10 }    validates :toppings, presence: true    validates :title, presence: true    belongs_to :user    belongs_to :steakend

A User has many Reviews and many Steaks through Reviews:

class User < ApplicationRecord    validates :username, presence: true, uniqueness: true    validates :fav_steak, presence: true    has_many :reviews    has_many :steaks, through: :reviewsend

A Steak has many Reviews and many Users through Reviews:

class Steak < ApplicationRecord    validates :restaurant, presence: true, uniqueness: true    has_many :reviews    has_many :users, through: :reviewsend

The models also use Active Record Validations to ensure that valid data is being saved in the database.

The site will ask the user to create an account with a username and password. Passwords are salted and hashed with BCrypt and securely stored in the database.

When the user successfully creates an account with a unique username, a session is created with the user's specific id inside the Users Controller:

def create    user = User.create!(user_params)    session[:user_id] = user.id    render json: user, status: :createdend

Inside the Application Controller, the private authorize method locates the current user by the :user_id held in session and assigns it to the instance variable @current_user, rendering an error response unless the variable is truthy - that is, the user exists:

before_action :authorizeprivatedef authorize    @current_user = User.find_by(id: session[:user_id])    render json: { error: ["Not authorized"] }, status: :unauthorized unless @current_userend

Note the before_action filter which is a method that runs before a controller action. As the other controllers inherit from Application Controller, this will ensure that the user is authorized to view the content which they have requested.

The application has four pages served by client-side routing - Home, Best Steak, My Reviews, and Add Steak.

Home acts as the landing page, rendering each instance of the Steak class as a card component. The card contains "Favorite" and "Review" buttons, conditionally rendered dependent on the user's reviews and favorite steak:
Home Page with Steak Cards
On the back end, when a user reviews a steak, the POST request points to the create method in the Reviews Controller:

class ReviewsController < ApplicationController    def create        review = @current_user.reviews.create!(review_params)        steak = Steak.find(params[:steak_id])        steak.update(rating: steak.calc_avg_rating)        render json: @current_user, status: :created    end    private    def review_params        params.permit(:steak_id, :title, :comment, :rating, :toppings)    endend

The method creates a new instance of the Review class that will be associated with the user stored in the @current_user variable.

The reviewed steak is found by accessing the [:steak_id] in the params hash. The steak's rating will be updated with the value returned by the instance method calc_avg_rating:

class Steak < ApplicationRecord    def calc_avg_rating        self.reviews.average(:rating)    endend

The method harnesses Active Record associations and methods to calculate the average rating from the steak's associated reviews.

The Best Steak page fetches from the API endpoint /steaks/highest-rated and renders the corresponding steak with its associated reviews. On the backend, the endpoint points to the highest_rated method in the Steaks Controller:

def highest_rated    max = Steak.maximum(:rating)    render json: Steak.where(rating: max)end

On the page, the steak's associated reviews are rendered as well thanks to the has_many :reviews relationship established in the Steak Serializer:

class SteakSerializer < ActiveModel::Serializer    attributes :id, :restaurant, :rating    has_many :reviewsend

Similarly, the My Reviews page displays Reviews associated with the current user instance utilizing the same association but in the User Serializer:

class UserSerializer < ActiveModel::Serializer  attributes :id, :username, :fav_steak  has_many :reviews  has_many :steaksend

Finally, on the Add Steak page the user can create a new instance of the Steak class and leave a corresponding review:
Add Steak Page
And there you have it, the Steak Whizard. Give it a spin and let me know your thoughts - who knows, you may even find your favorite Philly Cheesesteak along the way.


Original Link: https://dev.to/michaellobman/steak-whizard-2ikb

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To