2011-04-14 86 views
0

我有這個admin_controllerRuby on Rails:在會話中保存登錄狀態。

def login 
    if request.post? 
     if params[:full_name] == "adm" && params[:password] == "123" 
     data = { :success => 'true', :msg => "Welcome, #{params[:full_name]}"} 
     #redirect_to :action => :welcome 
     else 
     data = { :failure => 'true', :msg => "Username or Password wrong !"} 
     end 
     render :text => data.to_json, :layout => false 
    end 
    end 

    def welcome  
    end 

和我有login.js ......一切工作!

如何建立sessions

我不明白我的方式。請幫幫我。

我在頁面上有一些鏈接,當我登錄並點擊鏈接時,我會得到另一頁(New post),非常棒!但是,如果我回來,my application要我再次輸入登錄&密碼。如果我進入我的鏈接網址(New post)我沒有問題那裏,但它是錯誤的,必須有文字是這樣的:「Log in first

+0

它是什麼? 'return false' – fl00r 2011-04-21 12:38:10

+0

嗯,你在做什麼? – Will 2011-04-21 14:33:36

回答

1

簡短的回答:

session[:user_id] = id_of_user_that_just_signed_in 

application_controller.rb

class ApplicationController < ActionController::Base 
    protect_from_forgery 

    helper_method :current_user 

    private 
    def current_user 
     @current_user ||= User.find_by_id(session[:user_id]) if session[:user_id] 
    end 
end 

龍答:我寫了一個教程使用Omniauth和一個部分(「會話存儲」),你看到會話處理和寫入適當的助手:http://www.communityguides.eu/articles/16(與在Github上完整的代碼)