0
Q
獲得每個控制器
A
回答
2
可以在ApplicationController
添加代碼:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :current_user
def current_user
@user = session[:user]
end
end
0
@nash的前面回答是好的,這裏是爲您提供可以在每個方法/視圖使用的輔助方法替代。這就是Devise那樣的寶石去吧:
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
helper_method :user_signed_in?
private
def current_user
@current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
end
def user_signed_in?
return 1 if current_user
end
def authenticate_user!
if !current_user
flash[:error] = 'You need to sign in before accessing this page!'
redirect_to signin_services_path
end
end
end
相關問題
- 1. 獲得每個控制器的功能相同的數據
- 2. 獲得來自控制器
- 3. 獲得電流控制器
- 4. 對於每個控制器
- 5. 如何獲得比控制器
- 6. 在SpringMVC控制器中獲得404
- 7. 如何獲得「服務器控制檯」
- 8. 獲得從PartialView行動父控制器
- 9. Tsung如何獲得控制器ID
- 10. 沒能獲得ID在控制器
- 11. 無法從控制器獲得$ _post值
- 12. 如何獲得控制器partialview數據
- 13. 在控制器之外獲得原理
- 14. 獲得價值爲控制器
- 15. 如何獲得一個控制是另一個控制內部
- 16. 導軌5:加載每個控制器
- 17. 設計REDIRECT_URL每個控制器
- 18. 從控制器獲取Nhibernate ISession(每個會話請求)ASP.Net MVC
- 19. cakePHP從每個模型和控制器獲取變量
- 20. 每個UIView都需要一個控制器,還是一個控制器可以控制多個視圖?
- 21. 獲得在freetextbox控制
- 22. Kohana - 在每個頁面加載代碼(每個控制器)
- 23. 獲得第一個值在「點擊」並傳遞給控制器
- 24. 獲得一個嵌套模式的伯爵在控制器
- 25. 得讓值控制器
- 26. 得到笨控制器
- 27. 得到笨控制器
- 28. jquery。每個獲得值
- 29. Mootools的獲得從每個
- 30. 獲得每4個小時