开发者

RoR crash on save "You have a nil object and you didn't expect it"

i have a RoR object graph i'm saving. user has a list of sections. when a request comes in, i delete all current user sections, and repopulate with new ones.

def set_current
  @courses = params[:courses]
  user = current_user
  user.points = params[:points].nil? ? user.points : params[:points].to_i
  UserSection.delete(user.user_sections)
  @courses.each do |c|
    new_section = UserSection.new()
    new_section.user = user

    new_section.save
  end
  user.save

  head :ok
end

when i save, i get this:

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.new_record?):
  app/controllers/schedule_controller.rb:32:in `set_current'
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:162:in `start'
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'开发者_如何学JAVA
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:95:in `start'
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:92:in `each'
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:92:in `start'
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:23:in `start'
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/1.8/webrick/server.rb:82:in `start'
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.6/bin/rdebug-ide:82
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/lib/ruby/gems/1.8/gems/ruby-debug-ide-0.4.6/bin/rdebug-ide:19:in `load'
  /Applications/NetBeans/NetBeans 6.9.1.app/Contents/Resources/NetBeans/ruby/jruby-1.5.1/bin/rdebug-ide:19

thoughts on what's actually going on? the save happens successfully - makes it to the db and everything.

edit: the exception (schedule_controller.rb:32) is the call to user.save


Try replacing the first line with (if that's where your error is)

UserSection.delete(user.user_sections) if user.user_sections

But like Zabba said, code listings would be helpful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜