Add posts test
This commit is contained in:
parent
0df07c478a
commit
76229c8cc5
17 changed files with 278 additions and 0 deletions
70
app/controllers/posts_controller.rb
Normal file
70
app/controllers/posts_controller.rb
Normal file
|
|
@ -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
|
||||
2
app/helpers/posts_helper.rb
Normal file
2
app/helpers/posts_helper.rb
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
module PostsHelper
|
||||
end
|
||||
6
app/models/post.rb
Normal file
6
app/models/post.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class Post
|
||||
include Mongoid::Document
|
||||
include Mongoid::Timestamps
|
||||
field :title, type: String
|
||||
field :body, type: String
|
||||
end
|
||||
27
app/views/posts/_form.html.erb
Normal file
27
app/views/posts/_form.html.erb
Normal file
|
|
@ -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 :body, style: "display: block" %>
|
||||
<%= form.textarea :body %>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<%= form.submit %>
|
||||
</div>
|
||||
<% end %>
|
||||
12
app/views/posts/_post.html.erb
Normal file
12
app/views/posts/_post.html.erb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<div id="<%= dom_id post %>">
|
||||
<p>
|
||||
<strong>Title:</strong>
|
||||
<%= post.title %>
|
||||
</p>
|
||||
|
||||
<p>
|
||||
<strong>Body:</strong>
|
||||
<%= post.body %>
|
||||
</p>
|
||||
|
||||
</div>
|
||||
2
app/views/posts/_post.json.jbuilder
Normal file
2
app/views/posts/_post.json.jbuilder
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
json.extract! post, :id, :title, :body, :created_at, :updated_at
|
||||
json.url post_url(post, format: :json)
|
||||
12
app/views/posts/edit.html.erb
Normal file
12
app/views/posts/edit.html.erb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<% content_for :title, "Editing post" %>
|
||||
|
||||
<h1>Editing post</h1>
|
||||
|
||||
<%= render "form", post: @post %>
|
||||
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<%= link_to "Show this post", @post %> |
|
||||
<%= link_to "Back to posts", posts_path %>
|
||||
</div>
|
||||
16
app/views/posts/index.html.erb
Normal file
16
app/views/posts/index.html.erb
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<p style="color: green"><%= notice %></p>
|
||||
|
||||
<% content_for :title, "Posts" %>
|
||||
|
||||
<h1>Posts</h1>
|
||||
|
||||
<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 %>
|
||||
1
app/views/posts/index.json.jbuilder
Normal file
1
app/views/posts/index.json.jbuilder
Normal file
|
|
@ -0,0 +1 @@
|
|||
json.array! @posts, partial: "posts/post", as: :post
|
||||
11
app/views/posts/new.html.erb
Normal file
11
app/views/posts/new.html.erb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<% content_for :title, "New post" %>
|
||||
|
||||
<h1>New post</h1>
|
||||
|
||||
<%= render "form", post: @post %>
|
||||
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<%= link_to "Back to posts", posts_path %>
|
||||
</div>
|
||||
10
app/views/posts/show.html.erb
Normal file
10
app/views/posts/show.html.erb
Normal file
|
|
@ -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>
|
||||
1
app/views/posts/show.json.jbuilder
Normal file
1
app/views/posts/show.json.jbuilder
Normal file
|
|
@ -0,0 +1 @@
|
|||
json.partial! "posts/post", post: @post
|
||||
Loading…
Add table
Add a link
Reference in a new issue