Skip to content

Latest commit

 

History

History
126 lines (104 loc) · 12.6 KB

File metadata and controls

126 lines (104 loc) · 12.6 KB

Learn Ruby on Rails 8 Fast – Create a Real Web App in 1 Hour

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Introduction and Project Setup

  • Summary: The course recreates a real website like malle.com, featuring Stripe payments, OmniAuth login, pro-only posts, search, filters, and categories. Start by installing Rails 8, then run rails new malaki_rails_clone --database=postgresql --css=tailwind to set up the app with PostgreSQL and Tailwind CSS. Use rails g scaffold post title:string description:text thumbnail_url:string video_url:string pro:boolean to generate the Post model, then migrate with rails db:migrate and start the server with rails s.
  • Key Takeaway/Example: Rails scaffolds provide instant CRUD functionality out of the box, making it quick to build and test models like posts.
  • Link for More Details: Ask AI: Rails Project Setup

Adding Friendly ID for Slugs

  • Summary: To improve privacy and SEO, add the friendly_id gem to your Gemfile, run bundle install, then generate migrations like rails g migration add_slug_to_posts slug:uniq and rails g friendly_id. Update the Post model with extend FriendlyId and friendly_id :title, use: :slugged, and modify the controller's set_post to use friendly_find. This replaces numeric IDs in URLs with slugs based on the post title.
  • Key Takeaway/Example: After setup, new posts use titles in URLs, like /second-post instead of /posts/2.
  • Link for More Details: Ask AI: Friendly ID in Rails

Styling Posts and Adding Video Embeds

  • Summary: Generate a Pages controller with rails g controller pages home pricing privacy. Render a partial for post lists in the home view, adding Tailwind classes for grid layout. Create a helper method in posts_helper.rb to extract YouTube thumbnails from video URLs using regex. Include the Tailwind CDN in application.html.erb for development styling.
  • Key Takeaway/Example: The thumbnail helper parses YouTube IDs:
    def youtube_thumbnail(post)  
      id = post.video_url.match(/embed\/([\w-]+)/)[1]  
      "https://img.youtube.com/vi/#{id}/hqdefault.jpg"  
    end  
  • Link for More Details: Ask AI: Styling and YouTube Embeds in Rails

Creating Navbar, Footer, and Real-time Search

  • Summary: Create partials for _navbar.html.erb and _footer.html.erb, rendering them in the application layout. Add a search form in the navbar using Turbo and Stimulus for real-time filtering. In posts_controller.rb, filter posts with Post.where("title ILIKE ? OR description ILIKE ?", "%#{query}%", "%#{query}%") if a query param is present, and respond with Turbo Stream.
  • Key Takeaway/Example: The Stimulus controller submits the form on input:
    import { Controller } from "@hotwired/stimulus"  
    export default class extends Controller {  
      submit() { this.element.requestSubmit() }  
    }  
  • Link for More Details: Ask AI: Navbar and Real-time Search in Rails

Implementing User Authentication with Devise

  • Summary: Add devise gem, install with rails generate devise:install, and generate User model with additional columns like provider, uid, avatar_url. Seed an admin user in db/seeds.rb using Rails credentials for secure email/password storage. Use before_actions in controllers to authenticate and ensure admin access for post edits.
  • Key Takeaway/Example: Admin check in controller:
    def ensure_admin_user  
      unless current_user.email == Rails.application.credentials.dig(:admin, :email)  
        redirect_to root_path, alert: "You must be an admin to perform this action."  
      end  
    end  
  • Link for More Details: Ask AI: Devise Authentication in Rails

Adding OmniAuth for Google and GitHub

  • Summary: Add OmniAuth gems for Google and GitHub, configure in devise.rb with credentials. Set up routes and callbacks controller to handle auth. Update User model with devise :omniauthable and a from_omniauth method to create or find users based on provider data.
  • Key Takeaway/Example: Callback handling creates users with auth info like name and avatar.
  • Link for More Details: Ask AI: OmniAuth in Rails

Adding Categories and Filtering

  • Summary: Generate Category and Categorization models for many-to-many association. Add has_many :categories, through: :categorizations to Post. In forms, use checkboxes for categories; permit in params. Filter in index with joins if category_id param is present.
  • Key Takeaway/Example: Filter query: Post.joins(:categories).where(categories: { id: params[:category_id] }).distinct.
  • Link for More Details: Ask AI: Categories and Filtering in Rails

Enabling Rich Text for Descriptions

  • Summary: Run rails action_text:install, add has_rich_text :description to Post model, and change form fields to f.rich_text_area :description. This enables formatting like bold, italics, and images in post descriptions.
  • Key Takeaway/Example: Rich text preserves formatting in views automatically.
  • Link for More Details: Ask AI: Action Text in Rails

Restricting Pro Content and Styling Show Page

  • Summary: Add scopes to Post like scope :pro, -> { where(pro: true) }. In show views, check if post is pro and user is subscribed; otherwise, show a subscribe message. Style with Tailwind for better layout.
  • Key Takeaway/Example: Conditional rendering: If pro and not subscribed, hide content and prompt to subscribe.
  • Link for More Details: Ask AI: Pro Content Restriction in Rails

Implementing Stripe Payments

  • Summary: Add stripe gem, configure API key in initializer. Create pricing page with plans, routes for checkout sessions. In controller, create Stripe sessions based on plan (monthly/yearly) using price IDs from credentials. On success, update user subscribed status.
  • Key Takeaway/Example: Checkout session: Stripe::Checkout::Session.create({ customer_email: current_user.email, ... }).
  • Link for More Details: Ask AI: Stripe Payments in Rails

Deploying to Render

  • Summary: Push code to GitHub, create Render web service with Ruby, add build script for bundle and migrations. Configure production settings like cache store, add master key and database URL. Troubleshoot errors like PG connections or table duplicates by adjusting configs and redeploying.
  • Key Takeaway/Example: Build script: #!/usr/bin/env bash; bundle install; rails assets:precompile; rails assets:clean.
  • Link for More Details: Ask AI: Deploying Rails to Render

About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: