Skip to content

Commit 2862dbf

Browse files
author
wenjieyeo
committed
add 2 models
1 parent 2d4cc8f commit 2862dbf

35 files changed

+614
-1
lines changed

Diff for: app/assets/javascripts/microposts.coffee

+3
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/

Diff for: app/assets/javascripts/users.coffee

+3
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/

Diff for: app/assets/stylesheets/microposts.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the Microposts controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/

Diff for: app/assets/stylesheets/scaffolds.scss

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
margin: 33px;
8+
}
9+
10+
p, ol, ul, td {
11+
font-family: verdana, arial, helvetica, sans-serif;
12+
font-size: 13px;
13+
line-height: 18px;
14+
margin: 33px;
15+
}
16+
17+
pre {
18+
background-color: #eee;
19+
padding: 10px;
20+
font-size: 11px;
21+
}
22+
23+
a {
24+
color: #000;
25+
26+
&:visited {
27+
color: #666;
28+
}
29+
30+
&:hover {
31+
color: #fff;
32+
background-color: #000;
33+
}
34+
}
35+
36+
th {
37+
padding-bottom: 5px;
38+
}
39+
40+
td {
41+
padding-bottom: 7px;
42+
padding-left: 5px;
43+
padding-right: 5px;
44+
}
45+
46+
div {
47+
&.field, &.actions {
48+
margin-bottom: 10px;
49+
}
50+
}
51+
52+
#notice {
53+
color: green;
54+
}
55+
56+
.field_with_errors {
57+
padding: 2px;
58+
background-color: red;
59+
display: table;
60+
}
61+
62+
#error_explanation {
63+
width: 450px;
64+
border: 2px solid red;
65+
padding: 7px;
66+
padding-bottom: 0;
67+
margin-bottom: 20px;
68+
background-color: #f0f0f0;
69+
70+
h2 {
71+
text-align: left;
72+
font-weight: bold;
73+
padding: 5px 5px 5px 15px;
74+
font-size: 12px;
75+
margin: -7px;
76+
margin-bottom: 0;
77+
background-color: #c00;
78+
color: #fff;
79+
}
80+
81+
ul li {
82+
font-size: 12px;
83+
list-style: square;
84+
}
85+
}
86+
87+
label {
88+
display: block;
89+
}

Diff for: app/assets/stylesheets/users.scss

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the Users controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/

Diff for: app/controllers/microposts_controller.rb

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class MicropostsController < ApplicationController
2+
before_action :set_micropost, only: [:show, :edit, :update, :destroy]
3+
4+
# GET /microposts
5+
# GET /microposts.json
6+
def index
7+
@microposts = Micropost.all
8+
end
9+
10+
# GET /microposts/1
11+
# GET /microposts/1.json
12+
def show
13+
end
14+
15+
# GET /microposts/new
16+
def new
17+
@micropost = Micropost.new
18+
end
19+
20+
# GET /microposts/1/edit
21+
def edit
22+
end
23+
24+
# POST /microposts
25+
# POST /microposts.json
26+
def create
27+
@micropost = Micropost.new(micropost_params)
28+
29+
respond_to do |format|
30+
if @micropost.save
31+
format.html { redirect_to @micropost, notice: 'Micropost was successfully created.' }
32+
format.json { render :show, status: :created, location: @micropost }
33+
else
34+
format.html { render :new }
35+
format.json { render json: @micropost.errors, status: :unprocessable_entity }
36+
end
37+
end
38+
end
39+
40+
# PATCH/PUT /microposts/1
41+
# PATCH/PUT /microposts/1.json
42+
def update
43+
respond_to do |format|
44+
if @micropost.update(micropost_params)
45+
format.html { redirect_to @micropost, notice: 'Micropost was successfully updated.' }
46+
format.json { render :show, status: :ok, location: @micropost }
47+
else
48+
format.html { render :edit }
49+
format.json { render json: @micropost.errors, status: :unprocessable_entity }
50+
end
51+
end
52+
end
53+
54+
# DELETE /microposts/1
55+
# DELETE /microposts/1.json
56+
def destroy
57+
@micropost.destroy
58+
respond_to do |format|
59+
format.html { redirect_to microposts_url, notice: 'Micropost 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_micropost
67+
@micropost = Micropost.find(params[:id])
68+
end
69+
70+
# Never trust parameters from the scary internet, only allow the white list through.
71+
def micropost_params
72+
params.require(:micropost).permit(:content, :user_id)
73+
end
74+
end

Diff for: app/controllers/users_controller.rb

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
class UsersController < ApplicationController
2+
before_action :set_user, only: [:show, :edit, :update, :destroy]
3+
4+
# GET /users
5+
# GET /users.json
6+
def index
7+
@users = User.all
8+
end
9+
10+
# GET /users/1
11+
# GET /users/1.json
12+
def show
13+
end
14+
15+
# GET /users/new
16+
def new
17+
@user = User.new
18+
end
19+
20+
# GET /users/1/edit
21+
def edit
22+
end
23+
24+
# POST /users
25+
# POST /users.json
26+
def create
27+
@user = User.new(user_params)
28+
29+
respond_to do |format|
30+
if @user.save
31+
format.html { redirect_to @user, notice: 'User was successfully created.' }
32+
format.json { render :show, status: :created, location: @user }
33+
else
34+
format.html { render :new }
35+
format.json { render json: @user.errors, status: :unprocessable_entity }
36+
end
37+
end
38+
end
39+
40+
# PATCH/PUT /users/1
41+
# PATCH/PUT /users/1.json
42+
def update
43+
respond_to do |format|
44+
if @user.update(user_params)
45+
format.html { redirect_to @user, notice: 'User was successfully updated.' }
46+
format.json { render :show, status: :ok, location: @user }
47+
else
48+
format.html { render :edit }
49+
format.json { render json: @user.errors, status: :unprocessable_entity }
50+
end
51+
end
52+
end
53+
54+
# DELETE /users/1
55+
# DELETE /users/1.json
56+
def destroy
57+
@user.destroy
58+
respond_to do |format|
59+
format.html { redirect_to users_url, notice: 'User 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_user
67+
@user = User.find(params[:id])
68+
end
69+
70+
# Never trust parameters from the scary internet, only allow the white list through.
71+
def user_params
72+
params.require(:user).permit(:name, :email)
73+
end
74+
end

Diff for: app/helpers/microposts_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module MicropostsHelper
2+
end

Diff for: app/helpers/users_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module UsersHelper
2+
end

Diff for: app/models/micropost.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Micropost < ApplicationRecord
2+
belongs_to :user
3+
validates :content, length: { maximum: 140 }, presence: true
4+
end

Diff for: app/models/user.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class User < ApplicationRecord
2+
has_many :microposts
3+
validates name, presence: true
4+
validates email, presence: true
5+
end

Diff for: app/views/microposts/_form.html.erb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<%= form_for(micropost) do |f| %>
2+
<% if micropost.errors.any? %>
3+
<div id="error_explanation">
4+
<h2><%= pluralize(micropost.errors.count, "error") %> prohibited this micropost from being saved:</h2>
5+
6+
<ul>
7+
<% micropost.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 :content %>
16+
<%= f.text_area :content %>
17+
</div>
18+
19+
<div class="field">
20+
<%= f.label :user_id %>
21+
<%= f.number_field :user_id %>
22+
</div>
23+
24+
<div class="actions">
25+
<%= f.submit %>
26+
</div>
27+
<% end %>

Diff for: app/views/microposts/edit.html.erb

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<h1>Editing Micropost</h1>
2+
3+
<%= render 'form', micropost: @micropost %>
4+
5+
<%= link_to 'Show', @micropost %> |
6+
<%= link_to 'Back', microposts_path %>

Diff for: app/views/microposts/index.html.erb

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p id="notice"><%= notice %></p>
2+
3+
<h1>Microposts</h1>
4+
5+
<table>
6+
<thead>
7+
<tr>
8+
<th>Content</th>
9+
<th>User</th>
10+
<th colspan="3"></th>
11+
</tr>
12+
</thead>
13+
14+
<tbody>
15+
<% @microposts.each do |micropost| %>
16+
<tr>
17+
<td><%= micropost.content %></td>
18+
<td><%= micropost.user_id %></td>
19+
<td><%= link_to 'Show', micropost %></td>
20+
<td><%= link_to 'Edit', edit_micropost_path(micropost) %></td>
21+
<td><%= link_to 'Destroy', micropost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
22+
</tr>
23+
<% end %>
24+
</tbody>
25+
</table>
26+
27+
<br>
28+
29+
<%= link_to 'New Micropost', new_micropost_path %>

Diff for: app/views/microposts/index.json.jbuilder

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
json.array!(@microposts) do |micropost|
2+
json.extract! micropost, :id, :content, :user_id
3+
json.url micropost_url(micropost, format: :json)
4+
end

Diff for: app/views/microposts/new.html.erb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>New Micropost</h1>
2+
3+
<%= render 'form', micropost: @micropost %>
4+
5+
<%= link_to 'Back', microposts_path %>

Diff for: app/views/microposts/show.html.erb

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<p id="notice"><%= notice %></p>
2+
3+
<p>
4+
<strong>Content:</strong>
5+
<%= @micropost.content %>
6+
</p>
7+
8+
<p>
9+
<strong>User:</strong>
10+
<%= @micropost.user_id %>
11+
</p>
12+
13+
<%= link_to 'Edit', edit_micropost_path(@micropost) %> |
14+
<%= link_to 'Back', microposts_path %>

Diff for: app/views/microposts/show.json.jbuilder

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
json.extract! @micropost, :id, :content, :user_id, :created_at, :updated_at

Diff for: app/views/users/_form.html.erb

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<%= form_for(user) do |f| %>
2+
<% if user.errors.any? %>
3+
<div id="error_explanation">
4+
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
5+
6+
<ul>
7+
<% user.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 :name %>
16+
<%= f.text_field :name %>
17+
</div>
18+
19+
<div class="field">
20+
<%= f.label :email %>
21+
<%= f.text_field :email %>
22+
</div>
23+
24+
<div class="actions">
25+
<%= f.submit %>
26+
</div>
27+
<% end %>

0 commit comments

Comments
 (0)