helper는 view에서 자주 쓰이는 코드를 재사용하기 위한 method이다.(view에서만 사용가능)
application_controller는 모든 controller에서 상속 된다.
그래서 controller, view에서 모두 사용하고 싶으면 아래와 같은 방법으로 사용해주면 된다.
controllers/application_controller.rb
class ApplicationController < ActionController::Base
helper_method :current_user, :logged_in?
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
def logged_in?
!!current_user
end
end
'ruby on rails' 카테고리의 다른 글
Unit Tests, Functional Tests, Integration Tests (0) | 2020.11.13 |
---|---|
heroku rails console (0) | 2020.11.13 |
db table 생성, association 설정 (0) | 2020.11.12 |
rails5에 bootstrap 적용하기 (0) | 2020.11.11 |
rails6에 bootstrap 적용하기 (0) | 2020.11.11 |