-
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
15 changed files
with
202 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,14 @@ | ||
class HomeController < ApplicationController | ||
protect_from_forgery with: :null_session | ||
|
||
# curl -X GET http://localhost:3000/index\?foo\=yaro | ||
def index | ||
render plain: "hello world #{request.params[:foo]}" | ||
end | ||
|
||
# curl -X POST -d 'name=shm' http://localhost:3000/post_example | ||
def post_example | ||
# render plain: "hello world #{request.params[:foo]}" | ||
render plain: request.body.read | ||
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,76 @@ | ||
class PostsController < ApplicationController | ||
protect_from_forgery with: :null_session | ||
before_action :set_post, only: %i[ show edit update destroy ] | ||
|
||
# curl -X GET http://localhost:3000/posts.json | ||
# GET /posts or /posts.json | ||
def index | ||
@posts = Post.all | ||
end | ||
|
||
# curl -X GET http://localhost:3000/posts/2.json | ||
# 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 | ||
|
||
# curl -X POST -H "Content-Type: application/json" -d '{"post": {"title": "first post", "published": "false"}}' http://localhost:3000/posts.json | ||
# POST /posts or /posts.json | ||
def create | ||
@post = Post.new(post_params) | ||
|
||
respond_to do |format| | ||
if @post.save | ||
format.html { redirect_to post_url(@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 | ||
|
||
# curl -X PATCH -H "Content-Type: application/json" -d '{"post": {"title": "UPDATED post", "published": "true"}}' http://localhost:3000/posts/1.json | ||
# PATCH/PUT /posts/1 or /posts/1.json | ||
def update | ||
respond_to do |format| | ||
if @post.update(post_params) | ||
format.html { redirect_to post_url(@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 | ||
|
||
# curl -X DELETE -H "Content-Type: application/json" http://localhost:3000/posts/1.json | ||
# DELETE /posts/1 or /posts/1.json | ||
def destroy | ||
@post.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to posts_url, 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[:id]) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def post_params | ||
params.require(:post).permit(:title, :published) | ||
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,3 @@ | ||
class Post < ApplicationRecord | ||
broadcasts_to ->(post) { :posts_list } | ||
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) do |form| %> | ||
<% if post.errors.any? %> | ||
<div style="color: red"> | ||
<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> | ||
<%= form.label :title, style: "display: block" %> | ||
<%= form.text_field :title %> | ||
</div> | ||
|
||
<div> | ||
<%= form.label :published, style: "display: block" %> | ||
<%= form.check_box :published %> | ||
</div> | ||
|
||
<div> | ||
<%= form.submit %> | ||
</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> | ||
<strong>Title:</strong> | ||
<%= post.title %> | ||
</p> | ||
|
||
<p> | ||
<strong>Published:</strong> | ||
<%= post.published %> | ||
</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, :published, :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,10 @@ | ||
<h1>Editing post</h1> | ||
|
||
<%= render "form", post: @post %> | ||
|
||
<br> | ||
|
||
<div> | ||
<%= link_to "Show this post", @post %> | | ||
<%= link_to "Back to posts", posts_path %> | ||
</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,16 @@ | ||
<p style="color: green"><%= notice %></p> | ||
|
||
<h1>Posts</h1> | ||
|
||
<%= turbo_stream_from :posts_list %> | ||
|
||
<div id="posts"> | ||
<% @posts.each do |post| %> | ||
<%= render post %> | ||
<p> | ||
<%= link_to "Show this post", post %> | ||
</p> | ||
<% end %> | ||
</div> | ||
|
||
<%= link_to "New post", new_post_path %> |
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,9 @@ | ||
<h1>New post</h1> | ||
|
||
<%= render "form", post: @post %> | ||
|
||
<br> | ||
|
||
<div> | ||
<%= link_to "Back to posts", posts_path %> | ||
</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,10 @@ | ||
<p style="color: green"><%= notice %></p> | ||
|
||
<%= render @post %> | ||
|
||
<div> | ||
<%= link_to "Edit this post", edit_post_path(@post) %> | | ||
<%= link_to "Back to posts", posts_path %> | ||
<%= button_to "Destroy this post", @post, method: :delete %> | ||
</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[7.0] | ||
def change | ||
create_table :posts do |t| | ||
t.string :title | ||
t.boolean :published | ||
|
||
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.