current_user object is getting lost

Hello,

I’m working on an app that tracks oil/gas/electricity consumption. Can be seen on http://thetracker.carbontracking.com with login/password as guest/guest

This is all going fine. Now I decide that I also want to log water usage. I see that it’s the exact same as the method for gas, i.e. a meter that logs usage in m3, so I figure I’ll cut and paste as much code as possible.

I generate the scaffold and edit the existing view where I want to be able to define a new water account. This page is on http://thetracker.carbontracking.com/data_entry/index/new_account and is the same page used to create a new gas account.
When I click on the link to “New Water Account” I get a error as follows


You have a nil object when you didn't expect it!
The error occurred while evaluating nil.country

Now I’ve verified this using logger.debug as you can see in the code below. It does show that @current_user is null.

  
def edit
  logger.debug "Current user : #{@current_user}"
    @water_account = params[:id] ?   @current_user.water_accounts.find_by_id(params[:id]) : WaterAccount.new
    @water_suppliers = @current_user.country.water_suppliers.find( :all, :order => 'name')
    @water_units = WaterUnit.find( :all, :order => 'name')

What I can’t fathom is that on the same page for setting up new water/gas etc. accounts, the link to a new gas account works fine, and I copied the water_accounts_controller code directly from the gas_account_controller code, exactly the same. They are both accessed via the same view so I cant understand how in one case the @current_user is null and in the other case, its fine, and I can access it. BTW, the line that gives the error is the

  
   @water_suppliers = @current_user.country.water_suppliers.find( :all, :order => 'name')

I’m stumped by the fact that , from a controller point of view, the code is exactly the same, and the code in the view which accesses both controllers is also the same but in one case the current_user object disappears.

Any help would be most appreciated.

/ Colm

The very fact that I try to explain the problem clearly has led me on the right path.
From the scaffold my water_account_controller started with

class WaterAccountsController < ApplicationController 

whereas my gas_accounts_controlle started with

class WaterAccountsController < AuthenticatedController 

Ba-da-bing… another erro has surfaced now but at least I’m back on track…