views:

117

answers:

1

Newly added description: (sorry for not mentioning)

The ApplicationController.current_account is defined as:

class ApplicationController < ActionController::Base
  class << self
    def current_account
      @current_account
    end
    def current_account=(value)
      @current_account = value
    end
  end

=========

I encountered a strange performance in my current project, which is about session. The strange part is it was normal in Safari but failed in other browsers (includes chrome, firefox and opera).

There is a registration form for input of part of the key information (email, password, etc) and is submitted to an action called "create"

This is the basic code of create action:

@account = Account.new(params[:account])
if @account.save
  ApplicationController.current_account = @account
  session[:current_account] = ApplicationController.current_account
  session[:account] = ApplicationController.current_account.id

  email = @account.email
  Mailer.deliver_account_confirmation(email)

  flash[:type] = "success"
  flash[:notice] = "Successfully Created Account"

  redirect_to :controller => "accounts", :action => "create_step_2"
else
  flash[:type] = "error"
  flash[:title] = "Oops, something wasn't right."
  flash[:notice] = "Mistakes are marked below in red. Please fix them and resubmit the form. Thanks."
  render :action => "new"
end

Also I created a before_filter in the application controller, which has the following code:

ApplicationController.current_account = Account.find_by_id(session[:current_account].id) unless session[:current_account].blank?

For Safari, there is no any problem. But for the other browsers, the session[:current_account] does not exist and so produced the following error message:

RuntimeError in AccountsController#create_step_2

Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id

Please could anyone help me?

A: 

1] don't write

ApplicationController.current_account

Just

current_account

2] in your application_controller

   def current_account
     session[:current_account]
   end

3]

  ApplicationController.current_account = @account
  session[:current_account] = ApplicationController.current_account
  session[:account] = ApplicationController.current_account.id

should be

  session[:current_account] = @account
  session[:account] = @account.id
Salil
Sorry that I forgot to say that current_account is defined as: class ApplicationController << ActionController::Base class << self def current_account @current_account end def current_account=(value) @current_account = value end end ...So could I still use step 1 and 2 stated above?
PeterWong
sorry then @peter i am not sure then it will work for you.I never used like it before so not an idea bout it. Sorry again
Salil
I used class variable instead of instance variable in the ApplicationController because some validation codes in the account model will need the current_account. (ie. validates_xxx, :if=>:some_method_needed_current_account). I don't know how to pass instance variable to a model's validation.
PeterWong