Associate users and posts, add user show page

This commit is contained in:
yodalightsabr 2025-05-04 13:26:59 -07:00
parent 54d0932854
commit b6861b219d
10 changed files with 73 additions and 6 deletions

View file

@ -15,8 +15,9 @@ class PostsController < ApplicationController
@post = Post.new
end
def create
def create
@post = Post.new(post_params)
@post.user = current_user
if @post.save
redirect_to @post
else

View file

@ -0,0 +1,49 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ show ]
before_action :require_authentication, only: [:show]
# def index
# @users = User.all
# end
def show
@user = User.find(params[:id])
end
# def new
# @user = User.new
# end
# def create
# @user = user.new(user_params)
# if @user.save
# redirect_to @user
# else
# render :new, status: :unprocessable_entity
# end
# end
# def edit
# end
# def update
# if @user.update(user_params)
# redirect_to @user
# else
# render :edit, status: :unprocessable_entity
# end
# end
private
def set_user
@user = User.find(params[:id])
end
def user_params
params.expect(user: [ :email ])
end
end

View file

@ -1,6 +1,7 @@
class Post < ApplicationRecord
has_one_attached :featured_image
has_rich_text :description
belongs_to :user
validates :description, presence: true
validates :name, presence: true
validates :hours, presence: true, numericality: { less_than_or_equal_to: 5, greater_than_or_equal_to: 0 }

View file

@ -2,5 +2,5 @@ class User < ApplicationRecord
validates :email, uniqueness: true, presence: true, allow_nil: false
has_many :posts, dependent: :destroy
end

View file

@ -8,7 +8,7 @@
</div>
<p class="mb-2">
posted by <span class="text-[#96ABF9]">[name]</span>
posted by <span class="text-[#96ABF9]"><%= @post.user.id %></span>
<span class="opacity-50">// <%= time_ago_in_words(@post.created_at) %> ago // <%= pluralize(@post.hours, "hour") %> spent on </span>
<span class="text-[#96ABF9]">[name]</span>
</p>

View file

@ -10,7 +10,7 @@
<div class="text-center grid grid-cols-1 gap-2 text-xl font-dystopian">
<%= link_to "Main", posts_path, class: "#{current_page?(posts_path) ? 'bg-[#564CAD]' : 'hover:bg-[#564CAD]'} bg-[#2E2A54] p-2 rounded transition duration-100 block" %>
<%= link_to "User", root_path, class: "#{current_page?(root_path) ? 'bg-[#564CAD]' : 'hover:bg-[#564CAD]'} bg-[#2E2A54] p-2 rounded transition duration-100 block" %>
<%= link_to "User", user_path(current_user), class: "#{current_page?(user_path(current_user)) ? 'bg-[#564CAD]' : 'hover:bg-[#564CAD]'} bg-[#2E2A54] p-2 rounded transition duration-100 block" %>
<%= link_to "Leaderboards", root_path, class: "#{current_page?(root_path) ? 'bg-[#564CAD]' : 'hover:bg-[#564CAD]'} bg-[#2E2A54] p-2 rounded transition duration-100 block" %>
<%= link_to "Info", info_path, class: "#{current_page?(info_path) ? 'bg-[#564CAD]' : 'hover:bg-[#564CAD]'} bg-[#2E2A54] p-2 rounded transition duration-100 block" %>
</div>

View file

@ -0,0 +1,10 @@
<div class="bg-[#3A5CE1] rounded-md border-2 border-[#96ABF9] p-8 mx-24 mt-24 text-center">
<h1 class="text-2xl">Fiona Hackworth</h1>
<p>@fiona.hackworth</p>
<p class="opacity-70">
<%= pluralize(@user.posts.count, "post") %> created
<span>// <%= pluralize(9, "hour") %> spent on </span>
<p>
</div>

View file

@ -12,6 +12,7 @@ Rails.application.routes.draw do
# Defines the root path route ("/")
root "landing#index"
resources :posts
resources :users
get "/info", to: "info#show"

View file

@ -0,0 +1,5 @@
class AddUserIdToPosts < ActiveRecord::Migration[8.0]
def change
add_reference :posts, :user, null: false, foreign_key: true
end
end

4
site/db/schema.rb generated
View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.0].define(version: 2025_04_29_211703) do
ActiveRecord::Schema[8.0].define(version: 2025_05_04_201326) do
create_table "action_text_rich_texts", force: :cascade do |t|
t.string "name", null: false
t.text "body"
@ -54,7 +54,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_04_29_211703) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "hours"
t.integer "user_id"
t.integer "user_id", null: false
t.index ["user_id"], name: "index_posts_on_user_id"
end