Skip to content

Commit 05f03e1

Browse files
committed
Add posts model
1 parent 430a39e commit 05f03e1

File tree

19 files changed

+310
-1
lines changed

19 files changed

+310
-1
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
class PostsController < ApplicationController
2+
before_action :set_post, only: %i[ show edit update destroy ]
3+
4+
# GET /posts or /posts.json
5+
def index
6+
@posts = Post.all
7+
end
8+
9+
# GET /posts/1 or /posts/1.json
10+
def show
11+
end
12+
13+
# GET /posts/new
14+
def new
15+
@post = Post.new
16+
end
17+
18+
# GET /posts/1/edit
19+
def edit
20+
end
21+
22+
# POST /posts or /posts.json
23+
def create
24+
@post = Post.new(post_params)
25+
26+
respond_to do |format|
27+
if @post.save
28+
format.html { redirect_to @post, notice: "Post was successfully created." }
29+
format.json { render :show, status: :created, location: @post }
30+
else
31+
format.html { render :new, status: :unprocessable_entity }
32+
format.json { render json: @post.errors, status: :unprocessable_entity }
33+
end
34+
end
35+
end
36+
37+
# PATCH/PUT /posts/1 or /posts/1.json
38+
def update
39+
respond_to do |format|
40+
if @post.update(post_params)
41+
format.html { redirect_to @post, notice: "Post was successfully updated." }
42+
format.json { render :show, status: :ok, location: @post }
43+
else
44+
format.html { render :edit, status: :unprocessable_entity }
45+
format.json { render json: @post.errors, status: :unprocessable_entity }
46+
end
47+
end
48+
end
49+
50+
# DELETE /posts/1 or /posts/1.json
51+
def destroy
52+
@post.destroy!
53+
54+
respond_to do |format|
55+
format.html { redirect_to posts_path, status: :see_other, notice: "Post was successfully destroyed." }
56+
format.json { head :no_content }
57+
end
58+
end
59+
60+
private
61+
# Use callbacks to share common setup or constraints between actions.
62+
def set_post
63+
@post = Post.find(params.expect(:id))
64+
end
65+
66+
# Only allow a list of trusted parameters through.
67+
def post_params
68+
params.expect(post: [ :title, :body ])
69+
end
70+
end

app/helpers/posts_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module PostsHelper
2+
end

app/models/post.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class Post < ApplicationRecord
2+
end

app/views/posts/_form.html.erb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<%= form_with(model: post, class: "contents") do |form| %>
2+
<% if post.errors.any? %>
3+
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3">
4+
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2>
5+
6+
<ul>
7+
<% post.errors.each do |error| %>
8+
<li><%= error.full_message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="my-5">
15+
<%= form.label :title %>
16+
<%= form.text_field :title, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
17+
</div>
18+
19+
<div class="my-5">
20+
<%= form.label :body %>
21+
<%= form.textarea :body, rows: 4, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
22+
</div>
23+
24+
<div class="inline">
25+
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
26+
</div>
27+
<% end %>

app/views/posts/_post.html.erb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div id="<%= dom_id post %>">
2+
<p class="my-5">
3+
<strong class="block font-medium mb-1">Title:</strong>
4+
<%= post.title %>
5+
</p>
6+
7+
<p class="my-5">
8+
<strong class="block font-medium mb-1">Body:</strong>
9+
<%= post.body %>
10+
</p>
11+
12+
</div>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
json.extract! post, :id, :title, :body, :created_at, :updated_at
2+
json.url post_url(post, format: :json)

app/views/posts/edit.html.erb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<div class="mx-auto md:w-2/3 w-full">
2+
<h1 class="font-bold text-4xl">Editing post</h1>
3+
4+
<%= render "form", post: @post %>
5+
6+
<%= link_to "Show this post", @post, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
7+
<%= link_to "Back to posts", posts_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
8+
</div>

app/views/posts/index.html.erb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<div class="w-full">
2+
<% if notice.present? %>
3+
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
4+
<% end %>
5+
6+
<% content_for :title, "Posts" %>
7+
8+
<div class="flex justify-between items-center">
9+
<h1 class="font-bold text-4xl">Posts</h1>
10+
<%= link_to "New post", new_post_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
11+
</div>
12+
13+
<div id="posts" class="min-w-full">
14+
<% @posts.each do |post| %>
15+
<%= render post %>
16+
<p>
17+
<%= link_to "Show this post", post, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
18+
</p>
19+
<% end %>
20+
</div>
21+
</div>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.array! @posts, partial: "posts/post", as: :post

app/views/posts/new.html.erb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div class="mx-auto md:w-2/3 w-full">
2+
<h1 class="font-bold text-4xl">New post</h1>
3+
4+
<%= render "form", post: @post %>
5+
6+
<%= link_to "Back to posts", posts_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
7+
</div>

0 commit comments

Comments
 (0)