-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
310 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
class PostsController < ApplicationController | ||
before_action :set_post, only: %i[ show edit update destroy ] | ||
|
||
# GET /posts or /posts.json | ||
def index | ||
@posts = Post.all | ||
end | ||
|
||
# GET /posts/1 or /posts/1.json | ||
def show | ||
end | ||
|
||
# GET /posts/new | ||
def new | ||
@post = Post.new | ||
end | ||
|
||
# GET /posts/1/edit | ||
def edit | ||
end | ||
|
||
# POST /posts or /posts.json | ||
def create | ||
@post = Post.new(post_params) | ||
|
||
respond_to do |format| | ||
if @post.save | ||
format.html { redirect_to @post, notice: "Post was successfully created." } | ||
format.json { render :show, status: :created, location: @post } | ||
else | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: @post.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /posts/1 or /posts/1.json | ||
def update | ||
respond_to do |format| | ||
if @post.update(post_params) | ||
format.html { redirect_to @post, notice: "Post was successfully updated." } | ||
format.json { render :show, status: :ok, location: @post } | ||
else | ||
format.html { render :edit, status: :unprocessable_entity } | ||
format.json { render json: @post.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /posts/1 or /posts/1.json | ||
def destroy | ||
@post.destroy! | ||
|
||
respond_to do |format| | ||
format.html { redirect_to posts_path, status: :see_other, notice: "Post was successfully destroyed." } | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
# Use callbacks to share common setup or constraints between actions. | ||
def set_post | ||
@post = Post.find(params.expect(:id)) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def post_params | ||
params.expect(post: [ :title, :body ]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module PostsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Post < ApplicationRecord | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<%= form_with(model: post, class: "contents") do |form| %> | ||
<% if post.errors.any? %> | ||
<div id="error_explanation" class="bg-red-50 text-red-500 px-3 py-2 font-medium rounded-lg mt-3"> | ||
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2> | ||
|
||
<ul> | ||
<% post.errors.each do |error| %> | ||
<li><%= error.full_message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div class="my-5"> | ||
<%= form.label :title %> | ||
<%= form.text_field :title, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> | ||
</div> | ||
|
||
<div class="my-5"> | ||
<%= form.label :body %> | ||
<%= form.textarea :body, rows: 4, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %> | ||
</div> | ||
|
||
<div class="inline"> | ||
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div id="<%= dom_id post %>"> | ||
<p class="my-5"> | ||
<strong class="block font-medium mb-1">Title:</strong> | ||
<%= post.title %> | ||
</p> | ||
|
||
<p class="my-5"> | ||
<strong class="block font-medium mb-1">Body:</strong> | ||
<%= post.body %> | ||
</p> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
json.extract! post, :id, :title, :body, :created_at, :updated_at | ||
json.url post_url(post, format: :json) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<div class="mx-auto md:w-2/3 w-full"> | ||
<h1 class="font-bold text-4xl">Editing post</h1> | ||
|
||
<%= render "form", post: @post %> | ||
|
||
<%= link_to "Show this post", @post, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> | ||
<%= link_to "Back to posts", posts_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div class="w-full"> | ||
<% if notice.present? %> | ||
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p> | ||
<% end %> | ||
|
||
<% content_for :title, "Posts" %> | ||
|
||
<div class="flex justify-between items-center"> | ||
<h1 class="font-bold text-4xl">Posts</h1> | ||
<%= link_to "New post", new_post_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %> | ||
</div> | ||
|
||
<div id="posts" class="min-w-full"> | ||
<% @posts.each do |post| %> | ||
<%= render post %> | ||
<p> | ||
<%= link_to "Show this post", post, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> | ||
</p> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.array! @posts, partial: "posts/post", as: :post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div class="mx-auto md:w-2/3 w-full"> | ||
<h1 class="font-bold text-4xl">New post</h1> | ||
|
||
<%= render "form", post: @post %> | ||
|
||
<%= link_to "Back to posts", posts_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<div class="mx-auto md:w-2/3 w-full flex"> | ||
<div class="mx-auto"> | ||
<% if notice.present? %> | ||
<p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p> | ||
<% end %> | ||
|
||
<%= render @post %> | ||
|
||
<%= link_to "Edit this post", edit_post_path(@post), class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> | ||
<%= link_to "Back to posts", posts_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> | ||
<div class="inline-block ml-2"> | ||
<%= button_to "Destroy this post", @post, method: :delete, class: "mt-2 rounded-lg py-3 px-5 bg-gray-100 font-medium" %> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
json.partial! "posts/post", post: @post |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreatePosts < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :posts do |t| | ||
t.string :title | ||
t.text :body | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require "test_helper" | ||
|
||
class PostsControllerTest < ActionDispatch::IntegrationTest | ||
setup do | ||
@post = posts(:one) | ||
end | ||
|
||
test "should get index" do | ||
get posts_url | ||
assert_response :success | ||
end | ||
|
||
test "should get new" do | ||
get new_post_url | ||
assert_response :success | ||
end | ||
|
||
test "should create post" do | ||
assert_difference("Post.count") do | ||
post posts_url, params: { post: { body: @post.body, title: @post.title } } | ||
end | ||
|
||
assert_redirected_to post_url(Post.last) | ||
end | ||
|
||
test "should show post" do | ||
get post_url(@post) | ||
assert_response :success | ||
end | ||
|
||
test "should get edit" do | ||
get edit_post_url(@post) | ||
assert_response :success | ||
end | ||
|
||
test "should update post" do | ||
patch post_url(@post), params: { post: { body: @post.body, title: @post.title } } | ||
assert_redirected_to post_url(@post) | ||
end | ||
|
||
test "should destroy post" do | ||
assert_difference("Post.count", -1) do | ||
delete post_url(@post) | ||
end | ||
|
||
assert_redirected_to posts_url | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
title: MyString | ||
body: MyText | ||
|
||
two: | ||
title: MyString | ||
body: MyText |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "test_helper" | ||
|
||
class PostTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require "application_system_test_case" | ||
|
||
class PostsTest < ApplicationSystemTestCase | ||
setup do | ||
@post = posts(:one) | ||
end | ||
|
||
test "visiting the index" do | ||
visit posts_url | ||
assert_selector "h1", text: "Posts" | ||
end | ||
|
||
test "should create post" do | ||
visit posts_url | ||
click_on "New post" | ||
|
||
fill_in "Body", with: @post.body | ||
fill_in "Title", with: @post.title | ||
click_on "Create Post" | ||
|
||
assert_text "Post was successfully created" | ||
click_on "Back" | ||
end | ||
|
||
test "should update Post" do | ||
visit post_url(@post) | ||
click_on "Edit this post", match: :first | ||
|
||
fill_in "Body", with: @post.body | ||
fill_in "Title", with: @post.title | ||
click_on "Update Post" | ||
|
||
assert_text "Post was successfully updated" | ||
click_on "Back" | ||
end | ||
|
||
test "should destroy Post" do | ||
visit post_url(@post) | ||
click_on "Destroy this post", match: :first | ||
|
||
assert_text "Post was successfully destroyed" | ||
end | ||
end |