如何在注册后立即登录用户?

Jus*_*zer 2 ruby authentication ruby-on-rails registration ruby-on-rails-3

我知道我应该把代码放在用户控制器的create动作中,但我不确定应该放什么代码.我还假设它应该在我的会话控制器中调用create动作,但是我不知道如何...

顺便说一句,我render :template => 'sessions/create'在用户控制器的创建操作中尝试过,但在注册时出现此错误:

Template is missing

Missing template sessions/create with {:locale=>[:en, :en], :formats=>[:html], :handlers=>[:rjs, :rhtml, :erb, :rxml, :builder]} in view paths "/rubyprograms/dreamstill/app/views", "/rubyprograms/dreamstill/vendor/plugins/facebox_render/app/views"
Run Code Online (Sandbox Code Playgroud)

这都在我的应用程序控制器中:

protected 
  # Returns the currently logged in user or nil if there isn't one
  def current_user
    return unless session[:user_id]
    @current_user ||= User.find_by_id(session[:user_id]) 
  end


  # Make current_user available in templates as a helper
  helper_method :current_user

  # Filter method to enforce a login requirement
  # Apply as a before_filter on any controller you want to protect
  def authenticate
    logged_in? ? true : access_denied
  end

  # Predicate method to test for a logged in user    
  def logged_in?
    current_user.is_a? User
  end

  # Make logged_in? available in templates as a helper
  helper_method :logged_in?

  def access_denied
    respond_to do |format|
      format.html do
        flash[:alert] = "You must log in to peform this action."
        redirect_to root_path
      end

      format.js do
        render_to_facebox(:partial => 'sessions/login_box')
      end
    end
     false
  end
Run Code Online (Sandbox Code Playgroud)

mu *_*ort 5

在你的控制器的某个地方你有一些看起来像这样的东西:

user = User.new
# set attributes
user.save
render :template => 'sessions/create' # Probably based on your question
Run Code Online (Sandbox Code Playgroud)

您需要做的就是更新session到:

user = User.new
# set attributes
if(user.save)
   session[:user_id] = user.id
   # Send them somewhere useful
else
   # Handle the error
end
Run Code Online (Sandbox Code Playgroud)

他们已登录一次session[:user_id].