Welcome to Hot Blog, a demonstration application designed to explore and master Hotwire technologies in Rails 8. This app showcases real-time updates, seamless navigation, and dynamic interactions powered by Turbo Streams and Stimulus. Whether you're new to Hotwire or looking to deepen your understanding, this is the perfect place to experiment and learn. Let's get started!
..............................................................................
Post and Comment Functionality
Real-Time Updates with Turbo Streams
..............................................................................
Ruby 3.x
Rails 8.x
SQLite3 (or your preferred database)
Clone the repository:
git clone https://github.com/your-username/hot-blog.git
cd hot-blog
Install dependencies:
bundle install
Set up the database:
rails db:setup
Start the server:
rails server
Visit http://localhost:3000 in your browser.
rails new blog
mkdir app/views/about && rails generate controller about index
Update app/views/about/index.html.erb
:
<h1>Hot Blog v1</h1>
<p>The Hot Blog is a demonstration application designed to explore and master Hotwire technologies in Rails 8. This app showcases real-time updates, seamless navigation, and dynamic interactions powered by Turbo and Stimulus. Whether you're new to Hotwire or looking to deepen your understanding, this is the perfect place to experiment and learn. Welcome!</p>
mkdir app/views/main && rails generate controller main index
Update app/views/main/index.html.erb
:
<h1>Welcome to Hot Blog!</h1>
<p>Please Sign Up first!</p>
Update config/routes.rb:
Rails.application.routes.draw do
get "about", to: "about#index"
root "main#index"
end
rails generate model User email:string password_digest:string
rails db:migrate
Add has_secure_password
to app/models/user.rb
:
class User < ApplicationRecord
has_secure_password
end
Uncomment bcrypt
in your Gemfile:
gem "bcrypt", "~> 3.1.7"
Run bundle install.
Update app/models/user.rb
:
validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :password, presence: true, length: { minimum: 6 }, confirmation: true
In the Rails console:
User.create(email: "jay@gmail.com", password: "password", password_confirmation: "password")
rails generate scaffold post title body
rails db:migrate
bash
rails generate resource comment post:references user:references content:text
rails db:migrate
Update app/models/post.rb
:
class Post < ApplicationRecord
has_many :comments, dependent: :destroy
end
Update app/models/comment.rb
:
class Comment < ApplicationRecord
belongs_to :post
belongs_to :user
end
Update app/controllers/comments_controller.rb
:
class CommentsController < ApplicationController
before_action :set_post
def create
@comment = @post.comments.build(comment_params)
@comment.user = Current.user
if @comment.save
respond_to do |format|
format.turbo_stream
format.html { redirect_to @post, notice: "Comment created successfully!" }
end
else
redirect_to @post, alert: "Failed to create comment."
end
end
private
def set_post
@post = Post.find(params[:post_id])
end
def comment_params
params.require(:comment).permit(:content)
end
end
Create app/views/comments/create.turbo_stream.erb
:
<%= turbo_stream.append "comments", @comment %>
Update app/views/posts/show.html.erb
:
erb
<%= turbo_stream_from @post %>
<%= render @post %>
<strong>Comments:</strong>
<div id="comments">
<%= render @post.comments %>
</div>
<%= render "comments/new", post: @post %>
Update config/application.rb
:
config.time_zone = "America/Porto_Velho"
config.active_record.default_timezone = :local
Run:
bin/importmap pin local-time
Update app/javascript/application.js
:
javascript
import LocalTime from "local-time"
LocalTime.start()
Create app/models/current.rb
:
class Current < ActiveSupport::CurrentAttributes
attribute :user
end
Update app/controllers/application_controller.rb
:
class ApplicationController < ActionController::Base
before_action :set_current_user
private
def set_current_user
Current.user = User.find_by(id: session[:user_id]) if session[:user_id]
end
end
Update config/routes.rb
:
Rails.application.routes.draw do
resources :posts do
resources :comments
end
get "about", to: "about#index"
get "sign_up", to: "registrations#new"
post "sign_up", to: "registrations#create"
get "sign_in", to: "sessions#new"
post "sign_in", to: "sessions#create"
delete "sign_out", to: "sessions#destroy"
root "main#index"
end
Congratulations! You've built a real-time blog using Hotwire in Rails 8. This app demonstrates how to create dynamic, fast, and seamless user experiences with minimal code. Feel free to explore further and customize the app to suit your needs.
If you get stuck, check out the repository for reference.
Happy coding! 🚀
Let me know if you need further refinements or additional sections!