Skip to content

Commit

Permalink
Simple CRD for users.
Browse files Browse the repository at this point in the history
  • Loading branch information
winston committed Mar 1, 2016
1 parent 61784e4 commit 4a4723f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 1 deletion.
30 changes: 30 additions & 0 deletions app/controllers/users_controller.rb
@@ -0,0 +1,30 @@
class UsersController < ApplicationController
def index
@users = User.all
end

def new
@user = User.new
end

def create
@user = User.new(params.require(:user).permit(:name, :email))

if @user.save
redirect_to @user, notice: 'You have successfully created a user!'
else
render :new
end
end

def show
@user = User.find(params[:id])
end

def destroy
@user = User.find(params[:id])
@user.destroy

redirect_to users_path
end
end
4 changes: 4 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,4 @@
class User < ActiveRecord::Base
validates_presence_of :name, :email
validates_format_of :email, with: /@/
end
12 changes: 12 additions & 0 deletions app/views/users/index.html.erb
@@ -0,0 +1,12 @@
<div>
<%= link_to 'New User', new_user_path %>
</div>

<% @users.each do |user| %>
<div>
<div><%= link_to user.name, user %></div>
<div><%= user.email %></div>
<div><%= link_to 'Delete', user, method: :delete %></div>
</div>
<hr/>
<% end %>
23 changes: 23 additions & 0 deletions app/views/users/new.html.erb
@@ -0,0 +1,23 @@
<%= form_for @user do |f| %>
<% if @user.errors.any? %>
<div>
<ul>
<% @user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div>
<%= f.label :name %>
<%= f.text_field :name %>
</div>
<div>
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div>
<%= f.submit %>
</div>
<% end %>
5 changes: 5 additions & 0 deletions app/views/users/show.html.erb
@@ -0,0 +1,5 @@
<div>
<div><%= link_to @user.name, @user %></div>
<div><%= @user.email %></div>
<div><%= link_to 'Delete', @user, method: :delete %></div>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -2,6 +2,7 @@
root 'grams#index'

resources :grams
resources :users

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20160301145957_create_users.rb
@@ -0,0 +1,10 @@
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email

t.timestamps null: false
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160220131304) do
ActiveRecord::Schema.define(version: 20160301145957) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -24,4 +24,11 @@
t.datetime "updated_at", null: false
end

create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

end

0 comments on commit 4a4723f

Please sign in to comment.