Manuel da Costa
Manuel da Costa

Reputation: 107

how to create Rails To do list app with pre filled list?

There are a lot of tutorials on the internet about how to create a rails app where a user can create, edit, delete a task.

I need to create an app where there is a pre filled list of tasks with checkboxes that the user can tick off.

Being a noob to rails, I can't really find any resources to point me in the right direction.

Any help will be appreciated.

Thanks

Upvotes: 1

Views: 547

Answers (3)

Peter
Peter

Reputation: 132237

You should just add a method called prefill_todos! on User, then call that on creation. Here's a sketch of how this might look:

class User
  def prefill_todos!
    todos.create! name: "Buy Milk", deadline: (Date.today + 2.days)
    todos.create! name: "Use this app", complete: true
    todos.create! name: "Tell 3 others about this app", deadline: (Date.today + 1.week)
  end

  # Automatically add dummy todos after creating the object.
  # NOTE: Probably better to explicitly call user.prefill_todos! when you create user.
  after_create :prefill_todos!
end

Note: this answer only deals with the question of prefilling TODO items. You'll need to follow a separate tutorial (or ask a separate stack overflow question) covering that thing. Good luck!

Upvotes: 2

Andrew
Andrew

Reputation: 698

OK, so assuming the tutorials you are referring to save the created tasks to a database then you could create your task list app by following a tutorial and then use seeds.rb to pre-seed the database with the initial set of tasks. There is a tutorial on using seeds.rb on railscasts here. It's a little dated but is still relevant to rails 3.x

Upvotes: 0

Luís Ramalho
Luís Ramalho

Reputation: 10208

In order to create a check box with default true, you can use the following code in the migration:

class AddTaskToUsers < ActiveRecord::Migration
  def up
    add_column :users, :task, :boolean, :default => 1
  end

  def down
    remove_column :users, :task
  end
end

Upvotes: 0

Related Questions