Skip to content

Commit 7c8f17d

Browse files
committed
done with demo app
1 parent 7cd2860 commit 7c8f17d

29 files changed

+584
-0
lines changed
+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
class MicropostsController < ApplicationController
2+
# GET /microposts
3+
# GET /microposts.xml
4+
def index
5+
@microposts = Micropost.all
6+
7+
respond_to do |format|
8+
format.html # index.html.erb
9+
format.xml { render :xml => @microposts }
10+
end
11+
end
12+
13+
# GET /microposts/1
14+
# GET /microposts/1.xml
15+
def show
16+
@micropost = Micropost.find(params[:id])
17+
18+
respond_to do |format|
19+
format.html # show.html.erb
20+
format.xml { render :xml => @micropost }
21+
end
22+
end
23+
24+
# GET /microposts/new
25+
# GET /microposts/new.xml
26+
def new
27+
@micropost = Micropost.new
28+
29+
respond_to do |format|
30+
format.html # new.html.erb
31+
format.xml { render :xml => @micropost }
32+
end
33+
end
34+
35+
# GET /microposts/1/edit
36+
def edit
37+
@micropost = Micropost.find(params[:id])
38+
end
39+
40+
# POST /microposts
41+
# POST /microposts.xml
42+
def create
43+
@micropost = Micropost.new(params[:micropost])
44+
45+
respond_to do |format|
46+
if @micropost.save
47+
format.html { redirect_to(@micropost, :notice => 'Micropost was successfully created.') }
48+
format.xml { render :xml => @micropost, :status => :created, :location => @micropost }
49+
else
50+
format.html { render :action => "new" }
51+
format.xml { render :xml => @micropost.errors, :status => :unprocessable_entity }
52+
end
53+
end
54+
end
55+
56+
# PUT /microposts/1
57+
# PUT /microposts/1.xml
58+
def update
59+
@micropost = Micropost.find(params[:id])
60+
61+
respond_to do |format|
62+
if @micropost.update_attributes(params[:micropost])
63+
format.html { redirect_to(@micropost, :notice => 'Micropost was successfully updated.') }
64+
format.xml { head :ok }
65+
else
66+
format.html { render :action => "edit" }
67+
format.xml { render :xml => @micropost.errors, :status => :unprocessable_entity }
68+
end
69+
end
70+
end
71+
72+
# DELETE /microposts/1
73+
# DELETE /microposts/1.xml
74+
def destroy
75+
@micropost = Micropost.find(params[:id])
76+
@micropost.destroy
77+
78+
respond_to do |format|
79+
format.html { redirect_to(microposts_url) }
80+
format.xml { head :ok }
81+
end
82+
end
83+
end

app/controllers/users_controller.rb

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
class UsersController < ApplicationController
2+
# GET /users
3+
# GET /users.xml
4+
def index
5+
@users = User.all
6+
7+
respond_to do |format|
8+
format.html # index.html.erb
9+
format.xml { render :xml => @users }
10+
end
11+
end
12+
13+
# GET /users/1
14+
# GET /users/1.xml
15+
def show
16+
@user = User.find(params[:id])
17+
18+
respond_to do |format|
19+
format.html # show.html.erb
20+
format.xml { render :xml => @user }
21+
end
22+
end
23+
24+
# GET /users/new
25+
# GET /users/new.xml
26+
def new
27+
@user = User.new
28+
29+
respond_to do |format|
30+
format.html # new.html.erb
31+
format.xml { render :xml => @user }
32+
end
33+
end
34+
35+
# GET /users/1/edit
36+
def edit
37+
@user = User.find(params[:id])
38+
end
39+
40+
# POST /users
41+
# POST /users.xml
42+
def create
43+
@user = User.new(params[:user])
44+
45+
respond_to do |format|
46+
if @user.save
47+
format.html { redirect_to(@user, :notice => 'User was successfully created.') }
48+
format.xml { render :xml => @user, :status => :created, :location => @user }
49+
else
50+
format.html { render :action => "new" }
51+
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
52+
end
53+
end
54+
end
55+
56+
# PUT /users/1
57+
# PUT /users/1.xml
58+
def update
59+
@user = User.find(params[:id])
60+
61+
respond_to do |format|
62+
if @user.update_attributes(params[:user])
63+
format.html { redirect_to(@user, :notice => 'User was successfully updated.') }
64+
format.xml { head :ok }
65+
else
66+
format.html { render :action => "edit" }
67+
format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
68+
end
69+
end
70+
end
71+
72+
# DELETE /users/1
73+
# DELETE /users/1.xml
74+
def destroy
75+
@user = User.find(params[:id])
76+
@user.destroy
77+
78+
respond_to do |format|
79+
format.html { redirect_to(users_url) }
80+
format.xml { head :ok }
81+
end
82+
end
83+
end

app/helpers/microposts_helper.rb

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

app/helpers/users_helper.rb

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

app/models/micropost.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Micropost < ActiveRecord::Base
2+
validates :content, :length => { :maximun => 140 }
3+
end

app/models/user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class User < ActiveRecord::Base
2+
end

app/views/microposts/_form.html.erb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 |msg| %>
8+
<li><%= msg %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="field">
15+
<%= f.label :content %><br />
16+
<%= f.text_field :content %>
17+
</div>
18+
<div class="field">
19+
<%= f.label :user_id %><br />
20+
<%= f.text_field :user_id %>
21+
</div>
22+
<div class="actions">
23+
<%= f.submit %>
24+
</div>
25+
<% end %>

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' %>
4+
5+
<%= link_to 'Show', @micropost %> |
6+
<%= link_to 'Back', microposts_path %>

app/views/microposts/index.html.erb

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

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' %>
4+
5+
<%= link_to 'Back', microposts_path %>

app/views/microposts/show.html.erb

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

app/views/users/_form.html.erb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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 |msg| %>
8+
<li><%= msg %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="field">
15+
<%= f.label :name %><br />
16+
<%= f.text_field :name %>
17+
</div>
18+
<div class="field">
19+
<%= f.label :email %><br />
20+
<%= f.text_field :email %>
21+
</div>
22+
<div class="actions">
23+
<%= f.submit %>
24+
</div>
25+
<% end %>

app/views/users/edit.html.erb

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

app/views/users/index.html.erb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<h1>Listing users</h1>
2+
3+
<table>
4+
<tr>
5+
<th>Name</th>
6+
<th>Email</th>
7+
<th></th>
8+
<th></th>
9+
<th></th>
10+
</tr>
11+
12+
<% @users.each do |user| %>
13+
<tr>
14+
<td><%= user.name %></td>
15+
<td><%= user.email %></td>
16+
<td><%= link_to 'Show', user %></td>
17+
<td><%= link_to 'Edit', edit_user_path(user) %></td>
18+
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
19+
</tr>
20+
<% end %>
21+
</table>
22+
23+
<br />
24+
25+
<%= link_to 'New User', new_user_path %>

app/views/users/new.html.erb

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

app/views/users/show.html.erb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<p id="notice"><%= notice %></p>
2+
3+
<p>
4+
<b>Name:</b>
5+
<%= @user.name %>
6+
</p>
7+
8+
<p>
9+
<b>Email:</b>
10+
<%= @user.email %>
11+
</p>
12+
13+
14+
<%= link_to 'Edit', edit_user_path(@user) %> |
15+
<%= link_to 'Back', users_path %>

config/routes.rb

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
DemoApp::Application.routes.draw do
2+
resources :microposts
3+
4+
resources :users
5+
26
# The priority is based upon order of creation:
37
# first created -> highest priority.
48

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CreateUsers < ActiveRecord::Migration
2+
def self.up
3+
create_table :users do |t|
4+
t.string :name
5+
t.string :email
6+
7+
t.timestamps
8+
end
9+
end
10+
11+
def self.down
12+
drop_table :users
13+
end
14+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class CreateMicroposts < ActiveRecord::Migration
2+
def self.up
3+
create_table :microposts do |t|
4+
t.string :content
5+
t.integer :user_id
6+
7+
t.timestamps
8+
end
9+
end
10+
11+
def self.down
12+
drop_table :microposts
13+
end
14+
end

0 commit comments

Comments
 (0)