Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
186 lines (134 sloc) 4.47 KB
ToDo's List Demo App
This app will be setup on Rails 3.1.1 and use sqlite3 for the db. My system is running Ruby 1.9.2.
1. If using RVM create a new gemset $ rvm use 1.9.2@todo --create
2. Install rails in gemset. $ gem install rails --no-ri --no-rdoc
3. $ rails new todo
4. Create .rvmrc file in project directory
rvm use 1.9.2@todo
5. cd into project directory $ cd todo
6. Start the rails server $ rails s
If you see the rails welcome page your app works! It does nothing but it works!
7. open your text editor $ mate .
8. Lets look at the guts of a rails app
+this is where your migrations will be generated
+ This is where you add gems to the project
+ When you run bundle this is where all the gems and version numbers get added in to
-no longer holds the assets
-index.html (the rails welcome page)
-test (the default test unit files)
9. Time to load up the Gemfile
gem 'kaminari'
group :development do
gem 'nifty-generators'
gem 'rspec-rails'
group :test do
gem 'rspec'
gem 'mocha'
10. Bundle the gems $ bundle
11. install rspec $ rails g rspec:install
In spec_helper.rb change Mock Framework to mocha
12. Start git $ git init
If you like git flow also run $ git flow init
13. Time to create our todo's
$ rails g scaffold task item:string description:text
$ rake db:migrate
$ rails s
go to localhost:3000/tasks
14. Make task#index the root view in routes.rb
uncomment root :to
Change 'welcome#index' to 'tasks#index'
Remove public/index.html
restart the server if necessary $ rails s
go to localhost:/3000 and you should see the items index page
15. Use nifty-generators to create basic layout
$ rails g nifty:layout
In application.html.erb change javascript_include_tag to :application
Rename /public/stylesheets/application.css to nifty.css
Move nifty.css to app/assest/stylesheets
Remove some extra stuff in the views/layouts/application.html.erb
Give the <title> a title
remove title helper from <body>
Reload the page & you should have a nice looking page on a blue background
16. Add add priority to tasks
$ rails g migration add_priority_to_tasks priority:integer
Look at migration and see rails magic
$ rake db:migrate
Add priority fields to views/tasks/_form.html.erb
<div class="field">
<%= f.label :priority %><br />
<%= f.text_field :priority %>
Add priority to views/tasks/show.html.erb
In views/tasks.index.html.erb change description to priority
make task.item a link to show = link_to task.item, task
remove show link
17. Scaffold generates a bunch of tests on it's own. You probably want to get rid of them. Lets add some tests of our own
First prepare the test db
$ rake db:test:prepare
require 'spec_helper'
describe Task do
before(:each) do
@attr = {
:item => "test task",
:description => "test description",
:priority => 10
it "should create a new task given valid attributes" do
describe "task validations" do
it "should require an item" do =>""))
should_not be_valid
it "should require a priority" do =>""))
should_not be_valid
The task validations test we just added should fail. we will make them pass in the next step
$ rspec spec/models
18. Validate that the item & priority are not blank
validates_presence_of :item, :priority
rspec should pass
19. Seeding the db
1.upto(25) do |x|
Task.create(item: "Task #{x}", description: "Description #{x}", priority: x)
19. Making the default sort priority
default_scope :order => 'tasks.priority DESC'
20. Adding pagination with kaminari
def index
@tasks =[:page])
tasks/index.html.haml above the table add
= paginate @tasks
Changing the number of items being shown
def index
@tasks =[:page]).per(10)
Something went wrong with that request. Please try again.