Skip to content

Commit 2d7b74a

Browse files
committed
Add post model
1 parent 97babc1 commit 2d7b74a

19 files changed

Lines changed: 331 additions & 3 deletions

File tree

spec/dummy4/Gemfile.lock

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,3 @@ DEPENDENCIES
154154
turbolinks
155155
uglifier (>= 1.3.0)
156156
web-console (~> 2.0)
157-
158-
BUNDLED WITH
159-
1.10.5
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/
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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
24+
&:visited {
25+
color: #666;
26+
}
27+
28+
&:hover {
29+
color: #fff;
30+
background-color: #000;
31+
}
32+
}
33+
34+
div {
35+
&.field, &.actions {
36+
margin-bottom: 10px;
37+
}
38+
}
39+
40+
#notice {
41+
color: green;
42+
}
43+
44+
.field_with_errors {
45+
padding: 2px;
46+
background-color: red;
47+
display: table;
48+
}
49+
50+
#error_explanation {
51+
width: 450px;
52+
border: 2px solid red;
53+
padding: 7px;
54+
padding-bottom: 0;
55+
margin-bottom: 20px;
56+
background-color: #f0f0f0;
57+
58+
h2 {
59+
text-align: left;
60+
font-weight: bold;
61+
padding: 5px 5px 5px 15px;
62+
font-size: 12px;
63+
margin: -7px;
64+
margin-bottom: 0px;
65+
background-color: #c00;
66+
color: #fff;
67+
}
68+
69+
ul li {
70+
font-size: 12px;
71+
list-style: square;
72+
}
73+
}
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, :published)
73+
end
74+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module PostsHelper
2+
end

spec/dummy4/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
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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="field">
23+
<%= f.label :published %><br>
24+
<%= f.check_box :published %>
25+
</div>
26+
<div class="actions">
27+
<%= f.submit %>
28+
</div>
29+
<% end %>
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 %>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p id="notice"><%= notice %></p>
2+
3+
<h1>Listing Posts</h1>
4+
5+
<table>
6+
<thead>
7+
<tr>
8+
<th>Title</th>
9+
<th>Body</th>
10+
<th>Published</th>
11+
<th colspan="3"></th>
12+
</tr>
13+
</thead>
14+
15+
<tbody>
16+
<% @posts.each do |post| %>
17+
<tr>
18+
<td><%= post.title %></td>
19+
<td><%= post.body %></td>
20+
<td><%= post.published %></td>
21+
<td><%= link_to 'Show', post %></td>
22+
<td><%= link_to 'Edit', edit_post_path(post) %></td>
23+
<td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
24+
</tr>
25+
<% end %>
26+
</tbody>
27+
</table>
28+
29+
<br>
30+
31+
<%= link_to 'New Post', new_post_path %>

0 commit comments

Comments
 (0)