SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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...ex/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
    Code:
    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.
    Code Ruby:
    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
    Code Ruby:
       @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

  2. #2
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Code:
    class WaterAccountsController < ApplicationController
    whereas my gas_accounts_controlle started with

    Code:
    class WaterAccountsController < AuthenticatedController
    Ba-da-bing... another erro has surfaced now but at least I'm back on track.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •