Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/assets/javascripts/microposts.coffee
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/
3 changes: 3 additions & 0 deletions app/assets/javascripts/users.coffee
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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/microposts.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Microposts controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
84 changes: 84 additions & 0 deletions app/assets/stylesheets/scaffolds.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
body {
background-color: #fff;
color: #333;
margin: 33px;
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px;
}

pre {
background-color: #eee;
padding: 10px;
font-size: 11px;
}

a {
color: #000;

&:visited {
color: #666;
}

&:hover {
color: #fff;
background-color: #000;
}
}

th {
padding-bottom: 5px;
}

td {
padding: 0 5px 7px;
}

div {
&.field, &.actions {
margin-bottom: 10px;
}
}

#notice {
color: green;
}

.field_with_errors {
padding: 2px;
background-color: red;
display: table;
}

#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0;

h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff;
}

ul li {
font-size: 12px;
list-style: square;
}
}

label {
display: block;
}
3 changes: 3 additions & 0 deletions app/assets/stylesheets/users.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Users controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
70 changes: 70 additions & 0 deletions app/controllers/microposts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class MicropostsController < ApplicationController
before_action :set_micropost, only: %i[ show edit update destroy ]

# GET /microposts or /microposts.json
def index
@microposts = Micropost.all
end

# GET /microposts/1 or /microposts/1.json
def show
end

# GET /microposts/new
def new
@micropost = Micropost.new
end

# GET /microposts/1/edit
def edit
end

# POST /microposts or /microposts.json
def create
@micropost = Micropost.new(micropost_params)

respond_to do |format|
if @micropost.save
format.html { redirect_to micropost_url(@micropost), notice: "Micropost was successfully created." }
format.json { render :show, status: :created, location: @micropost }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @micropost.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /microposts/1 or /microposts/1.json
def update
respond_to do |format|
if @micropost.update(micropost_params)
format.html { redirect_to micropost_url(@micropost), notice: "Micropost was successfully updated." }
format.json { render :show, status: :ok, location: @micropost }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @micropost.errors, status: :unprocessable_entity }
end
end
end

# DELETE /microposts/1 or /microposts/1.json
def destroy
@micropost.destroy

respond_to do |format|
format.html { redirect_to microposts_url, notice: "Micropost was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_micropost
@micropost = Micropost.find(params[:id])
end

# Only allow a list of trusted parameters through.
def micropost_params
params.require(:micropost).permit(:content, :user_id)
end
end
70 changes: 70 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ show edit update destroy ]

# GET /users or /users.json
def index
@users = User.all
end

# GET /users/1 or /users/1.json
def show
end

# GET /users/new
def new
@user = User.new
end

# GET /users/1/edit
def edit
end

# POST /users or /users.json
def create
@user = User.new(user_params)

respond_to do |format|
if @user.save
format.html { redirect_to user_url(@user), notice: "User was successfully created." }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /users/1 or /users/1.json
def update
respond_to do |format|
if @user.update(user_params)
format.html { redirect_to user_url(@user), notice: "User was successfully updated." }
format.json { render :show, status: :ok, location: @user }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

# DELETE /users/1 or /users/1.json
def destroy
@user.destroy

respond_to do |format|
format.html { redirect_to users_url, notice: "User was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_user
@user = User.find(params[:id])
end

# Only allow a list of trusted parameters through.
def user_params
params.require(:user).permit(:name, :email)
end
end
2 changes: 2 additions & 0 deletions app/helpers/microposts_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module MicropostsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UsersHelper
end
5 changes: 5 additions & 0 deletions app/models/micropost.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Micropost < ApplicationRecord
belongs_to :user
validates :content, length: { maximum: 140 },
presence: true
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class User < ApplicationRecord
has_many :microposts
validates :name , presence: true
validates :email , presence: true
end
27 changes: 27 additions & 0 deletions app/views/microposts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= form_with(model: micropost, local: true) do |form| %>
<% if micropost.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(micropost.errors.count, "error") %> prohibited this micropost from being saved:</h2>

<ul>
<% micropost.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :content %>
<%= form.text_area :content %>
</div>

<div class="field">
<%= form.label :user_id %>
<%= form.number_field :user_id %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/microposts/_micropost.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! micropost, :id, :content, :user_id, :created_at, :updated_at
json.url micropost_url(micropost, format: :json)
6 changes: 6 additions & 0 deletions app/views/microposts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Micropost</h1>

<%= render 'form', micropost: @micropost %>

<%= link_to 'Show', @micropost %> |
<%= link_to 'Back', microposts_path %>
29 changes: 29 additions & 0 deletions app/views/microposts/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<p id="notice"><%= notice %></p>

<h1>Microposts</h1>

<table>
<thead>
<tr>
<th>Content</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @microposts.each do |micropost| %>
<tr>
<td><%= micropost.content %></td>
<td><%= micropost.user_id %></td>
<td><%= link_to 'Show', micropost %></td>
<td><%= link_to 'Edit', edit_micropost_path(micropost) %></td>
<td><%= link_to 'Destroy', micropost, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Micropost', new_micropost_path %>
1 change: 1 addition & 0 deletions app/views/microposts/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.array! @microposts, partial: "microposts/micropost", as: :micropost
5 changes: 5 additions & 0 deletions app/views/microposts/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Micropost</h1>

<%= render 'form', micropost: @micropost %>

<%= link_to 'Back', microposts_path %>
14 changes: 14 additions & 0 deletions app/views/microposts/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Content:</strong>
<%= @micropost.content %>
</p>

<p>
<strong>User:</strong>
<%= @micropost.user_id %>
</p>

<%= link_to 'Edit', edit_micropost_path(@micropost) %> |
<%= link_to 'Back', microposts_path %>
1 change: 1 addition & 0 deletions app/views/microposts/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.partial! "microposts/micropost", micropost: @micropost
27 changes: 27 additions & 0 deletions app/views/users/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<%= form_with(model: user, local: true) do |form| %>
<% if user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>

<ul>
<% user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :name %>
<%= form.text_field :name %>
</div>

<div class="field">
<%= form.label :email %>
<%= form.text_field :email %>
</div>

<div class="actions">
<%= form.submit %>
</div>
<% end %>
2 changes: 2 additions & 0 deletions app/views/users/_user.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
json.extract! user, :id, :name, :email, :created_at, :updated_at
json.url user_url(user, format: :json)
6 changes: 6 additions & 0 deletions app/views/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing User</h1>

<%= render 'form', user: @user %>

<%= link_to 'Show', @user %> |
<%= link_to 'Back', users_path %>
Loading