Skip to content

Commit dcddbc0

Browse files
committed
Committing result of be rails g scaffold post title:string body:text
1 parent e52a13e commit dcddbc0

File tree

19 files changed

+316
-0
lines changed

19 files changed

+316
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://coffeescript.org/

app/assets/stylesheets/posts.css.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the posts controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
body {
2+
background-color: #fff;
3+
color: #333;
4+
font-family: verdana, arial, helvetica, sans-serif;
5+
font-size: 13px;
6+
line-height: 18px;
7+
}
8+
9+
p, ol, ul, td {
10+
font-family: verdana, arial, helvetica, sans-serif;
11+
font-size: 13px;
12+
line-height: 18px;
13+
}
14+
15+
pre {
16+
background-color: #eee;
17+
padding: 10px;
18+
font-size: 11px;
19+
}
20+
21+
a {
22+
color: #000;
23+
&:visited {
24+
color: #666;
25+
}
26+
&:hover {
27+
color: #fff;
28+
background-color: #000;
29+
}
30+
}
31+
32+
div {
33+
&.field, &.actions {
34+
margin-bottom: 10px;
35+
}
36+
}
37+
38+
#notice {
39+
color: green;
40+
}
41+
42+
.field_with_errors {
43+
padding: 2px;
44+
background-color: red;
45+
display: table;
46+
}
47+
48+
#error_explanation {
49+
width: 450px;
50+
border: 2px solid red;
51+
padding: 7px;
52+
padding-bottom: 0;
53+
margin-bottom: 20px;
54+
background-color: #f0f0f0;
55+
h2 {
56+
text-align: left;
57+
font-weight: bold;
58+
padding: 5px 5px 5px 15px;
59+
font-size: 12px;
60+
margin: -7px;
61+
margin-bottom: 0px;
62+
background-color: #c00;
63+
color: #fff;
64+
}
65+
ul li {
66+
font-size: 12px;
67+
list-style: square;
68+
}
69+
}

app/controllers/posts_controller.rb

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

app/views/posts/_form.html.erb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<%= form_for(@post) do |f| %>
2+
<% if @post.errors.any? %>
3+
<div id="error_explanation">
4+
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
5+
6+
<ul>
7+
<% @post.errors.full_messages.each do |message| %>
8+
<li><%= message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="field">
15+
<%= f.label :title %><br>
16+
<%= f.text_field :title %>
17+
</div>
18+
<div class="field">
19+
<%= f.label :body %><br>
20+
<%= f.text_area :body %>
21+
</div>
22+
<div class="actions">
23+
<%= f.submit %>
24+
</div>
25+
<% end %>

app/views/posts/edit.html.erb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Editing post</h1>
2+
3+
<%= render 'form' %>
4+
5+
<%= link_to 'Show', @post %> |
6+
<%= link_to 'Back', posts_path %>

app/views/posts/index.html.erb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<h1>Listing posts</h1>
2+
3+
<table>
4+
<thead>
5+
<tr>
6+
<th>Title</th>
7+
<th>Body</th>
8+
<th colspan="3"></th>
9+
</tr>
10+
</thead>
11+
12+
<tbody>
13+
<% @posts.each do |post| %>
14+
<tr>
15+
<td><%= post.title %></td>
16+
<td><%= post.body %></td>
17+
<td><%= link_to 'Show', post %></td>
18+
<td><%= link_to 'Edit', edit_post_path(post) %></td>
19+
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
20+
</tr>
21+
<% end %>
22+
</tbody>
23+
</table>
24+
25+
<br>
26+
27+
<%= link_to 'New Post', new_post_path %>

app/views/posts/index.json.jbuilder

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
json.array!(@posts) do |post|
2+
json.extract! post, :id, :title, :body
3+
json.url post_url(post, format: :json)
4+
end

app/views/posts/new.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>New post</h1>
2+
3+
<%= render 'form' %>
4+
5+
<%= link_to 'Back', posts_path %>

app/views/posts/show.html.erb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<p id="notice"><%= notice %></p>
2+
3+
<p>
4+
<strong>Title:</strong>
5+
<%= @post.title %>
6+
</p>
7+
8+
<p>
9+
<strong>Body:</strong>
10+
<%= @post.body %>
11+
</p>
12+
13+
<%= link_to 'Edit', edit_post_path(@post) %> |
14+
<%= link_to 'Back', posts_path %>

app/views/posts/show.json.jbuilder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.extract! @post, :id, :title, :body, :created_at, :updated_at

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
Rails.application.routes.draw do
2+
resources :posts
3+
24
# The priority is based upon order of creation: first created -> highest priority.
35
# See how all your routes lay out with "rake routes".
46

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreatePosts < ActiveRecord::Migration
2+
def change
3+
create_table :posts do |t|
4+
t.string :title
5+
t.text :body
6+
7+
t.timestamps
8+
end
9+
end
10+
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'test_helper'
2+
3+
class PostsControllerTest < ActionController::TestCase
4+
setup do
5+
@post = posts(:one)
6+
end
7+
8+
test "should get index" do
9+
get :index
10+
assert_response :success
11+
assert_not_nil assigns(:posts)
12+
end
13+
14+
test "should get new" do
15+
get :new
16+
assert_response :success
17+
end
18+
19+
test "should create post" do
20+
assert_difference('Post.count') do
21+
post :create, post: { body: @post.body, title: @post.title }
22+
end
23+
24+
assert_redirected_to post_path(assigns(:post))
25+
end
26+
27+
test "should show post" do
28+
get :show, id: @post
29+
assert_response :success
30+
end
31+
32+
test "should get edit" do
33+
get :edit, id: @post
34+
assert_response :success
35+
end
36+
37+
test "should update post" do
38+
patch :update, id: @post, post: { body: @post.body, title: @post.title }
39+
assert_redirected_to post_path(assigns(:post))
40+
end
41+
42+
test "should destroy post" do
43+
assert_difference('Post.count', -1) do
44+
delete :destroy, id: @post
45+
end
46+
47+
assert_redirected_to posts_path
48+
end
49+
end

test/fixtures/posts.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
2+
3+
one:
4+
title: MyString
5+
body: MyText
6+
7+
two:
8+
title: MyString
9+
body: MyText

test/helpers/posts_helper_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
require 'test_helper'
2+
3+
class PostsHelperTest < ActionView::TestCase
4+
end

test/models/post_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'test_helper'
2+
3+
class PostTest < ActiveSupport::TestCase
4+
# test "the truth" do
5+
# assert true
6+
# end
7+
end

0 commit comments

Comments
 (0)