开发者

lost session after redirect_to

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 i开发者_开发百科d of nil, use object_id

Please could anyone help me?


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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜