problem in _todo partial

Subscribe to problem in _todo partial 5 post(s), 2 voice(s)

 
Avatar beta 7 post(s)

hi, its a rather particular circumstance… but if I

1. add a task
2. add the task to the todo list
3. remove the task from the todo list
4. add the task to the todo list again
5. delete the task from the task list (when it is still sitting in the todo list for the second time)

I get this error:

ActionView::TemplateError (Called id for nil, which would mistakenly be 4—if you really wanted the id of nil, use object_id) on line #3 of app/views/today/_todo.rhtml:

1: <l id="todo_" i><= todo.id %>”>
2: <
= link_to_remote image_tag(“check.gif”, :title => “Mark Complete”),
3: :url => {:controller => “task”, :action => “mark_complete”, :id => todo.task.id } >
4: <
if todo.task.complete >
5: <strike><
= todo.task.name ></strike>
6: < else %>

 
Avatar EldonAlameda Administrator 216 post(s)

Is your Task model using the version from the book or the correct one from the source / errata (see http://www.railsprojects.com/forums/3/topics/17)?

If it’s missing the :dependent => :destroy option that would cause that.

 
Avatar beta 7 post(s)

my Task model has the line:

has_one :todo, :dependent => :destroy

Which i saw in the errata.
However in the source code I downloaded this line is written as:

has_one :todo, :dependent => true

 
Avatar EldonAlameda Administrator 216 post(s)

both work in the 1.2 version of rails (don’t think they do in 2.0) – however the :dependent => :destroy became the preferred approach. I think it’s because it also allows you a little more flexibilty. i.e. you can also set it to :dependent => :nullify to remove the foreign_id yet keep the record.

 
Avatar EldonAlameda Administrator 216 post(s)

Actually just went back and tested your report and you’re right there is a bug there. In my haste I didn’t notice that by scoping the delete it wasn’t really deleting the todo but merely nullifying the schedule_id from that todo.

In all actuality there is no need to scope that search (i.e. making sure it’s associated with the current users schedule ) for the todo object twice.

Changing the delete method in the todo controller to this – should fix this bug

  def destroy 
     todo = current_user.schedule.todos.find(params[:id]) 
     Todo.delete(todo) 
     redirect_to(:controller => "today", :action => "index")
  end 

I’ve updated the errata here on the site. Thanks for catching this!!