-
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.
rails g scaffold Post title:text body:text user:references
- Loading branch information
Showing
18 changed files
with
260 additions
and
0 deletions.
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,3 @@ | ||
# Place all the behaviors and hooks related to the matching controller here. | ||
# All this logic will automatically be available in application.js. | ||
# You can use CoffeeScript in this file: http://coffeescript.org/ |
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 @@ | ||
// Place all the styles related to the Posts controller here. | ||
// They will automatically be included in application.css. | ||
// You can use Sass (SCSS) here: http://sass-lang.com/ |
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,74 @@ | ||
class PostsController < ApplicationController | ||
before_action :set_post, only: [:show, :edit, :update, :destroy] | ||
|
||
# GET /posts | ||
# GET /posts.json | ||
def index | ||
@posts = Post.all | ||
end | ||
|
||
# GET /posts/1 | ||
# GET /posts/1.json | ||
def show | ||
end | ||
|
||
# GET /posts/new | ||
def new | ||
@post = Post.new | ||
end | ||
|
||
# GET /posts/1/edit | ||
def edit | ||
end | ||
|
||
# POST /posts | ||
# POST /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 } | ||
format.json { render json: @post.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PATCH/PUT /posts/1 | ||
# PATCH/PUT /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 } | ||
format.json { render json: @post.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /posts/1 | ||
# DELETE /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 | ||
|
||
# Never trust parameters from the scary internet, only allow the white list through. | ||
def post_params | ||
params.require(:post).permit(:title, :body, :user_id) | ||
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,3 @@ | ||
class Post < ApplicationRecord | ||
belongs_to :user | ||
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,32 @@ | ||
<%= form_for(post) do |f| %> | ||
<% if post.errors.any? %> | ||
<div id="error_explanation"> | ||
<h2><%= pluralize(post.errors.count, "error") %> prohibited this post from being saved:</h2> | ||
|
||
<ul> | ||
<% post.errors.full_messages.each do |message| %> | ||
<li><%= message %></li> | ||
<% end %> | ||
</ul> | ||
</div> | ||
<% end %> | ||
|
||
<div class="field"> | ||
<%= f.label :title %> | ||
<%= f.text_area :title %> | ||
</div> | ||
|
||
<div class="field"> | ||
<%= f.label :body %> | ||
<%= f.text_area :body %> | ||
</div> | ||
|
||
<div class="field"> | ||
<%= f.label :user_id %> | ||
<%= f.text_field :user_id %> | ||
</div> | ||
|
||
<div class="actions"> | ||
<%= f.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,2 @@ | ||
json.extract! post, :id, :title, :body, :user_id, :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,6 @@ | ||
<h1>Editing Post</h1> | ||
|
||
<%= render 'form', post: @post %> | ||
|
||
<%= link_to 'Show', @post %> | | ||
<%= link_to 'Back', posts_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,31 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<h1>Posts</h1> | ||
|
||
<table> | ||
<thead> | ||
<tr> | ||
<th>Title</th> | ||
<th>Body</th> | ||
<th>User</th> | ||
<th colspan="3"></th> | ||
</tr> | ||
</thead> | ||
|
||
<tbody> | ||
<% @posts.each do |post| %> | ||
<tr> | ||
<td><%= post.title %></td> | ||
<td><%= post.body %></td> | ||
<td><%= post.user %></td> | ||
<td><%= link_to 'Show', post %></td> | ||
<td><%= link_to 'Edit', edit_post_path(post) %></td> | ||
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<br> | ||
|
||
<%= 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,5 @@ | ||
<h1>New Post</h1> | ||
|
||
<%= render 'form', post: @post %> | ||
|
||
<%= link_to 'Back', posts_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,19 @@ | ||
<p id="notice"><%= notice %></p> | ||
|
||
<p> | ||
<strong>Title:</strong> | ||
<%= @post.title %> | ||
</p> | ||
|
||
<p> | ||
<strong>Body:</strong> | ||
<%= @post.body %> | ||
</p> | ||
|
||
<p> | ||
<strong>User:</strong> | ||
<%= @post.user %> | ||
</p> | ||
|
||
<%= link_to 'Edit', edit_post_path(@post) %> | | ||
<%= link_to 'Back', posts_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.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
Rails.application.routes.draw do | ||
resources :posts | ||
resources :users | ||
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
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,11 @@ | ||
class CreatePosts < ActiveRecord::Migration[5.0] | ||
def change | ||
create_table :posts do |t| | ||
t.text :title | ||
t.text :body | ||
t.references :user, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
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,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, user_id: @post.user_id } } | ||
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, user_id: @post.user_id } } | ||
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,11 @@ | ||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
title: MyText | ||
body: MyText | ||
user: one | ||
|
||
two: | ||
title: MyText | ||
body: MyText | ||
user: two |
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 |