Skip to content

Commit

Permalink
Added rails Todo scaffold
Browse files Browse the repository at this point in the history
  • Loading branch information
froots committed Mar 15, 2011
1 parent 8aa5044 commit 06f1b61
Show file tree
Hide file tree
Showing 20 changed files with 376 additions and 237 deletions.
87 changes: 87 additions & 0 deletions app/controllers/todos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class TodosController < ApplicationController
# GET /todos
# GET /todos.xml
def index
@todos = Todo.all

respond_to do |format|
format.html # index.html.erb
format.json { render :json => @todos }
format.xml { render :xml => @todos }
end
end

# GET /todos/1
# GET /todos/1.xml
def show
@todo = Todo.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render :json => @todo }
format.xml { render :xml => @todo }
end
end

# GET /todos/new
# GET /todos/new.xml
def new
@todo = Todo.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @todo }
end
end

# GET /todos/1/edit
def edit
@todo = Todo.find(params[:id])
end

# POST /todos
# POST /todos.xml
def create
@todo = Todo.new(params[:todo])

respond_to do |format|
if @todo.save
format.html { redirect_to(@todo, :notice => 'Todo was successfully created.') }
format.json { render :json => @todo, :status => :created, :location => @todo }
format.xml { render :xml => @todo, :status => :created, :location => @todo }
else
format.html { render :action => "new" }
format.json { render :json => @todo.errors, :status => :unprocessable_entity }
format.xml { render :xml => @todo.errors, :status => :unprocessable_entity }
end
end
end

# PUT /todos/1
# PUT /todos/1.xml
def update
@todo = Todo.find(params[:id])

respond_to do |format|
if @todo.update_attributes(params[:todo])
format.html { redirect_to(@todo, :notice => 'Todo was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @todo.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /todos/1
# DELETE /todos/1.xml
def destroy
@todo = Todo.find(params[:id])
@todo.destroy

respond_to do |format|
format.html { redirect_to(todos_url) }
format.xml { head :ok }
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/todos_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module TodosHelper
end
2 changes: 2 additions & 0 deletions app/models/todo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Todo < ActiveRecord::Base
end
33 changes: 33 additions & 0 deletions app/views/todos/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%= form_for(@todo) do |f| %>
<% if @todo.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@todo.errors.count, "error") %> prohibited this todo from being saved:</h2>

<ul>
<% @todo.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :priority %><br />
<%= f.text_field :priority %>
</div>
<div class="field">
<%= f.label :position %><br />
<%= f.text_field :position %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/todos/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing todo</h1>

<%= render 'form' %>
<%= link_to 'Show', @todo %> |
<%= link_to 'Back', todos_path %>
29 changes: 29 additions & 0 deletions app/views/todos/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<h1>Listing todos</h1>

<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>Priority</th>
<th>Position</th>
<th></th>
<th></th>
<th></th>
</tr>

<% @todos.each do |todo| %>
<tr>
<td><%= todo.title %></td>
<td><%= todo.description %></td>
<td><%= todo.priority %></td>
<td><%= todo.position %></td>
<td><%= link_to 'Show', todo %></td>
<td><%= link_to 'Edit', edit_todo_path(todo) %></td>
<td><%= link_to 'Destroy', todo, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>

<br />

<%= link_to 'New Todo', new_todo_path %>
5 changes: 5 additions & 0 deletions app/views/todos/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New todo</h1>

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

<p>
<b>Title:</b>
<%= @todo.title %>
</p>

<p>
<b>Description:</b>
<%= @todo.description %>
</p>

<p>
<b>Priority:</b>
<%= @todo.priority %>
</p>

<p>
<b>Position:</b>
<%= @todo.position %>
</p>


<%= link_to 'Edit', edit_todo_path(@todo) %> |
<%= link_to 'Back', todos_path %>
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@ class Application < Rails::Application

# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]

ActiveRecord::Base.include_root_in_json = false
end
end
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
BackboneJasmineExamples::Application.routes.draw do
resources :todos

# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20110315073827_create_todos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateTodos < ActiveRecord::Migration
def self.up
create_table :todos do |t|
t.string :title
t.text :description
t.integer :priority
t.integer :position

t.timestamps
end
end

def self.down
drop_table :todos
end
end
24 changes: 24 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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 => 20110315073827) do

create_table "todos", :force => true do |t|
t.string "title"
t.text "description"
t.integer "priority"
t.integer "position"
t.datetime "created_at"
t.datetime "updated_at"
end

end
Loading

0 comments on commit 06f1b61

Please sign in to comment.