Permalink
Browse files

Add people CRUD

  • Loading branch information...
1 parent 3f980f0 commit b9c52740a7346518c3b8d58009e97e8aa41bd8d8 @MarceloCajueiro committed Apr 18, 2012
@@ -0,0 +1,83 @@
+class PeopleController < ApplicationController
+ # GET /people
+ # GET /people.json
+ def index
+ @people = Person.all
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render json: @people }
+ end
+ end
+
+ # GET /people/1
+ # GET /people/1.json
+ def show
+ @person = Person.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render json: @person }
+ end
+ end
+
+ # GET /people/new
+ # GET /people/new.json
+ def new
+ @person = Person.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render json: @person }
+ end
+ end
+
+ # GET /people/1/edit
+ def edit
+ @person = Person.find(params[:id])
+ end
+
+ # POST /people
+ # POST /people.json
+ def create
+ @person = Person.new(params[:person])
+
+ respond_to do |format|
+ if @person.save
+ format.html { redirect_to @person, notice: 'Person was successfully created.' }
+ format.json { render json: @person, status: :created, location: @person }
+ else
+ format.html { render action: "new" }
+ format.json { render json: @person.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /people/1
+ # PUT /people/1.json
+ def update
+ @person = Person.find(params[:id])
+
+ respond_to do |format|
+ if @person.update_attributes(params[:person])
+ format.html { redirect_to @person, notice: 'Person was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render action: "edit" }
+ format.json { render json: @person.errors, status: :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /people/1
+ # DELETE /people/1.json
+ def destroy
+ @person = Person.find(params[:id])
+ @person.destroy
+
+ respond_to do |format|
+ format.html { redirect_to people_url }
+ format.json { head :no_content }
+ end
+ end
+end
View
@@ -0,0 +1,2 @@
+class Person < ActiveRecord::Base
+end
@@ -0,0 +1,14 @@
+<%= simple_form_for(@person, :html => { :class => 'form-vertical' }) do |f| %>
+ <%= f.error_notification %>
+
+ <div class="form-inputs">
+ <%= f.input :name %>
+ <%= f.input :phone %>
+ <%= f.input :birth_date, :as => :string %>
+ <%= f.input :document %>
+ </div>
+
+ <div class="form-actions">
+ <%= f.button :submit %>
+ </div>
+<% end %>
@@ -0,0 +1,6 @@
+<h1>Editing person</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @person %> |
+<%= link_to 'Back', people_path %>
@@ -0,0 +1,29 @@
+<h1>Listing people</h1>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Phone</th>
+ <th>Birth date</th>
+ <th>Document</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+<% @people.each do |person| %>
+ <tr>
+ <td><%= person.name %></td>
+ <td><%= person.phone %></td>
+ <td><%= person.birth_date %></td>
+ <td><%= person.document %></td>
+ <td><%= link_to 'Show', person %></td>
+ <td><%= link_to 'Edit', edit_person_path(person) %></td>
+ <td><%= link_to 'Destroy', person, confirm: 'Are you sure?', method: :delete %></td>
+ </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Person', new_person_path %>
@@ -0,0 +1,5 @@
+<h1>New person</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', people_path %>
@@ -0,0 +1,25 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+ <b>Name:</b>
+ <%= @person.name %>
+</p>
+
+<p>
+ <b>Phone:</b>
+ <%= @person.phone %>
+</p>
+
+<p>
+ <b>Birth date:</b>
+ <%= @person.birth_date %>
+</p>
+
+<p>
+ <b>Document:</b>
+ <%= @person.document %>
+</p>
+
+
+<%= link_to 'Edit', edit_person_path(@person) %> |
+<%= link_to 'Back', people_path %>
View
@@ -1,4 +1,6 @@
MaskValidatorExample::Application.routes.draw do
+ resources :people
+
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -0,0 +1,12 @@
+class CreatePeople < ActiveRecord::Migration
+ def change
+ create_table :people do |t|
+ t.string :name
+ t.string :phone
+ t.date :birth_date
+ t.string :document
+
+ t.timestamps
+ end
+ end
+end
View
@@ -0,0 +1,25 @@
+# encoding: UTF-8
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20120417234612) do
+
+ create_table "people", :force => true do |t|
+ t.string "name"
+ t.string "phone"
+ t.date "birth_date"
+ t.string "document"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ end
+
+end

0 comments on commit b9c5274

Please sign in to comment.