redirect_to之后丢失了会话

时间:2010-06-08 05:47:36

标签: ruby-on-rails session

最新添加的说明:(抱歉没有提及)

ApplicationController.current_account定义为:

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

======

我在当前的项目中遇到了一个奇怪的表现,那就是会话。奇怪的是它在Safari中是正常的但在其他浏览器中失败(包括chrome,firefox和opera)。

有一个注册表格用于输入部分关键信息(电子邮件,密码等),并提交给名为“创建”的行动

这是创建动作的基本代码:

@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

我还在应用程序控制器中创建了一个before_filter,其中包含以下代码:

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

对于Safari,没有任何问题。但对于其他浏览器,会话[:current_account]不存在,因此产生以下错误消息:

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

请有人帮助我吗?

1 个答案:

答案 0 :(得分:0)

1]不写

ApplicationController.current_account

只是

current_account
你的application_controller中的

2]

   def current_account
     session[:current_account]
   end

3]

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

应该是

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