Ruby class 2011 02 22

From Noisebridge
Jump to: navigation, search


[edit] A real rails app

[edit] What should we build?

A todo list application:

[edit] Models:

  • Task
    • id (integer, automatically generated)
    • timestamps (datetime, automatically maintained)
    • title (string)
    • description (string)
    • time_required (integer, 1-6)
    • value (integer, 1-10)
    • due_date (datetime)
    • done_date (datetime)
    • priority_item (boolean)
  • Project
    • id
    • timestamps
    • name
    • description
  • Tag
    • id
    • timestamps
    • name
    • description

[edit] Views:

Just one:

  • Task list
    • A table of tasks
    • Entry fields at the top and bottom for new tasks

If we get around to it we can do:

  • Projects & Tags page
  • Project view
  • Tag view

[edit] Controllers:


  • show
  • create
  • done

[edit] Let's begin

Run the following:

$ rails new rudo
$ cd rudo
$ rails generate model task

Then add the following lines to create_tasks migration:

create_table :tasks do |t|
  t.string      :title
  t.string      :description
  t.integer     :time_required
  t.integer     :value
  t.datetime    :due_date
  t.datetime    :done_date
  t.boolean     :priority_item
  t.references  :project
  # ^^ this is the same as t.integer  :project_id


Alright, now let's create the other migrations:

$ rails generate model project name:string description:string
$ rails generate model tag name:string description:string
$ rails generate model tagging

Now let's take a look at the migrations... See the fields automatically added to the migrations We'll need to modify the create_taggings migration, though:

create_table :taggings, :id => false do |t|
  # can also be t.integer  :tag_id, :task_id
  t.references  :tag
  t.references  :task


Okay, let's try to migrate:

$ rake db:migrate

Alright, now let's get our models associated:

[edit] app/models/task.rb

has_many    :taggings
has_many    :tags, :through => :taggings
belongs_to  :project

[edit] app/models/tagging.rb

belongs_to  :tags
belongs_to  :task

[edit] app/models/project.rb

has_many    :tasks

And now our controller:

$ rails generate controller tasks

Edit the controller and add the methods we're going to want:

[edit] app/controllers/tasks_controller.rb

def show
  @tasks = Task.all

def create
  @task =[:task])

def done

And now for our primary view:

[edit] app/views/task/show.html.erb

<h1> Tasks: </h1>
<% @tasks.each do |task| %>
  <h3><%= task.title %></h3>
  Description: <%= task.description %></br>
  Due: <%= task.due_date %></br>
  Priority Item: <%= task.priority_item %></br>
  Project: <%= %></br>
  Tags: <%= task.tags %></br>
<% end %>

Okay, so now what? Ah, yes, routes. First:

$ rm public/index.html

Now, inside our routes file:

[edit] config/routes.rb

root :to => "tasks#show"

Alright, so now we have our view, but not much to see. Let's add some tasks from the console. But why not make it possible to do it from our page, seeing as how that's how it will get used. Let's look back at our scratch project.

Personal tools