Skip to content

Building a todo list with Rails 3 – Part Two

EDIT: The source code is now on Github.

Welcome to Part 2 of the video tutorial on building a todo list with Rails 3. Part One of the video tutorial is here.

Please leave comments and your suggestions for future topics to cover.

If you enjoyed this post, you should sign up to be notified when my book, Real World Development with Ruby on Rails, is released in May 2013. The book and full source code to the application we develop in it will be just $39 with added value packages also available.

22 Comments Post a comment
  1. Nick #

    awesome tutorial!

    November 6, 2010
  2. I really liked this video. I’ve read about named scopes but never really understood their basic use case until I watched this. I liked how you didn’t just run rails g scaffold but instead built it by hand showing each part and why it falls where it does in the process. I never knew about the routes nesting that was done for me behind the scenes either. Thanks!

    I would very much like a third part on added javascript to your todooly. :-)

    January 4, 2011
  3. A really great tutorial! :)

    July 10, 2011
  4. Great tutorial, I’m just starting out on Rails and this is the only tutorial i have found that really gives you an understanding of what’s going on, without going so slow that you are bored to death. I’m looking forward to any others you do in the future A+!!!

    July 22, 2011
  5. Hey, I thought this tutorial was great.

    I took it a step further and made adding tasks ajaxed and also put ajax filtering for complete/incomplete tasks.

    I would like to make a video and stuff so if you could post the source as a github project I could start the video from getting the project from github and the code would be right where you left it and continue it.

    Cheers

    September 4, 2011
    • Robin Fisher #

      Hi Joel,

      Good idea. Check my Github account this weekend and I’ll put it up there.

      Robin

      September 5, 2011
  6. Daniel Arzuaga #

    Great tutorial Robin, you are a great teacher man.

    I’m pretty new to rails and i was able to understand everything and follow along with no problems.

    Keep em coming

    September 6, 2011
  7. Ryan #

    Great tutorial! Really learning a lot! Just running into one thing -

    in routes.rb I have:

    resources :lists do
    resources :tasks
    end

    match ‘lists/:list_id/tasks/:id/complete’ => ‘tasks#complete’, :as => :complete_task

    root :to=> “lists#index”

    But I get this error when trying to use the app:

    No route matches {:list_id=>1, :controller=>”tasks”, :action=>”complete”, :id=>nil}

    What did I screw up on?

    Thanks

    September 15, 2011
    • Robin Fisher #

      It appears that the id for the task is not being passed in. Your code should be:

      complete_task_path(@list.id,task.id)

      Are you passing the task.id into the link? Note there is no @ symbol before it in this case.

      Robin

      September 15, 2011
  8. Ryan #

    Is my button code if that helps, it looks like it is all there to me, really confusing me!

    September 15, 2011
  9. Ryan #

    %= button_to “Complete”, complete_task_path(@list.id,task.id) % – brackets are by % signs, not showing up though

    September 15, 2011
  10. Johnny #

    I got the same error Ryan did. I think the problem is in the controller? (Disclosure: not a rails dev)

    It seems the task created by:

    def show
    @list = List.find(params[:id])
    @task = @list.tasks.new
    end

    is appended to the @list.tasks, and shows up as an extra list item task in the lists/show view?

    I’m on OSX Snow Leopard, Ruby 1.8.7, Rails 3.1.1 and followed the tutorial to a “t” (character by character, watched everything 3 times) could it be a version/gem idiosyncrasy somewhere?

    October 25, 2011
  11. Johnny #

    I was able to get the new task item on the show list page to work correctly for me:

    i changed my ListController show method from:

    def show
    @list = List.find(params[:id])
    @task = @list.tasks.new
    end

    to :

    def show
    @list = List.find(params[:id])
    @task = @list.tasks.new
    end

    and my list/show page form from

    form_for [@list, @task] do |form|

    to :

    form_for [@list, @list.tasks.new] do |form|

    and boom! like magic. Any insights to why that might be happening?

    October 25, 2011
    • Robin Fisher #

      Hi Johnny! Yes, does look like the new task is being appended to the list but because it’s not been created, it doesn’t have an id and that’s why the error is thrown. It may have been a change in Rails that causes initialised but not created objects to be added to a collection.

      October 25, 2011
  12. Hi Robin! This is my first attempt at any rails code EVER! I’ve been following along quite smoothly until I hit the same error message as Ryan and Johnny.

    ….. No route matches {:controller=>”tasks”, :action=>”complete”, :list_id=>2, :id=>nil}

    I have rails v 3.1.3 Johnny posted a fix which I attempted to implement but it does not seem to work for me – the error message remains exactly the same.

    Could Johnny perhaps have made an error in his post? His fix for the ListController Show method doesn’t seem to change your code in any way.

    QUOTE:

    i changed my ListController show method from:

    def show
    @list = List.find(params[:id])
    @task = @list.tasks.new
    end

    to :

    def show
    @list = List.find(params[:id])
    @task = @list.tasks.new
    end

    Those two bits seem to be identical?

    Please help – I was so enjoying the tutorial and would love to complete it before my holiday is over :)

    Thanks a million!

    December 30, 2011
  13. I know it’s been said but your videos are just fantastic. Thanks for putting this together, it’s really helpful, well explained and paced perfectly.

    January 28, 2012
  14. @Sonya

    I had the same issue and I did what Johnny had suggested and in one of his suggestions he did have the ‘show’ action duplicated. so, instead of this:

    def show
    @list = List.find(params[:id])
    @task = @list.tasks.new
    end

    CHANGE TO:
    def show
    @list = List.find(params[:id])
    end

    THEN CHANGE:
    form_for [@list, @task] do |form|

    TO:
    form_for [@list, @list.tasks.new] do |form|

    Hope it helps ;)

    BTW, Robin, thanks for this great Tutorial ;)

    May 11, 2012
  15. Gil #

    Thanks Tony/Johnny! Can anyone explain further what exactly was causing this issue? I’ve spent hours reading through SO thinking it had something to do with not using .build for a form with associations…that turned out to be wrong. Does anyone have any explanations?

    Thanks again Robin for this tutorial! This tutorial has really clarified and driven home most of the concepts I learned in my beginners tutorial.

    August 17, 2012
  16. Russ #

    Great tutorial! I am trying to create a button to delete a task but having troubles in figuring out which link to use. Was trying to use something sort of like this but did not work, says couldnt find list with id=. Any advice? Thanks
    :delete %>

    October 12, 2012
  17. Russ #

    Actually, I figured out a way to do it that works for me. I basically mimicked the format used to mark a task as complete, but had it as remove. So far, it has been working for me. Thanks!

    October 17, 2012
  18. Russ #

    Actually, is there an easy way to edit the tasks that are created? Thanks!

    October 17, 2012
  19. Robin #

    Russ, the easiest way to edit a task would be to use something like jQuery so that when you click on a task, it changes to an input box with the contents of that box then being submitted back to the server via Ajax. The alternative would be to have a standard edit form and link from the task to the edit form so you can change it.

    October 21, 2012

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 168 other followers