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
-App
-Assets
+images
+javascripts
+stylesheets
-Controllers
-Helpers
-Mailers
-Models
-Views
-Config
+routes.rb
-DB
+this is where your migrations will be generated
-Gemfile
+ This is where you add gems to the project
-Gemfile.lock
+ When you run bundle this is where all the gems and version numbers get added in to
-Public
-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'
end
group :test do
gem 'rspec'
gem 'mocha'
end
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 %>
</div>
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
spec/models/task_spec.rb
require 'spec_helper'
describe Task do
before(:each) do
@attr = {
:item => "test task",
:description => "test description",
:priority => 10
}
end
it "should create a new task given valid attributes" do
Task.create!(@attr)
end
describe "task validations" do
it "should require an item" do
Task.new(@attr.merge(:item =>""))
should_not be_valid
end
it "should require a priority" do
Task.new(@attr.merge(:priority =>""))
should_not be_valid
end
end
end
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
models/task.rb
validates_presence_of :item, :priority
rspec should pass
19. Seeding the db
db/seeds.rb
1.upto(25) do |x|
Task.create(item: "Task #{x}", description: "Description #{x}", priority: x)
end
19. Making the default sort priority
models/task.rb
default_scope :order => 'tasks.priority DESC'
20. Adding pagination with kaminari
tasks_controller.rb
def index
@tasks = Task.order.page(params[:page])
end
tasks/index.html.haml above the table add
= paginate @tasks
Changing the number of items being shown
def index
@tasks = Task.order.page(params[:page]).per(10)
end
Something went wrong with that request. Please try again.