Created
March 27, 2012 03:03
-
-
Save shu0115/2212130 to your computer and use it in GitHub Desktop.
controllerとviewの両方からメソッドを呼びたい場合
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
◆コントローラに書いたメソッドをhelper_methodにする | |
controllers/application_controller.rb | |
----- | |
private | |
def current_user | |
@current_user ||= User.where( id: session[:user_id] ).first | |
end | |
helper_method :current_user | |
----- | |
◆ビューから呼び出す | |
views/layouts/application.html.erb | |
----- | |
<% if current_user %> | |
〜 | |
<% else %> | |
〜 | |
<% end %> | |
----- | |
◆コントローラから呼び出す | |
----- | |
item = Item.where( user_id: current_user.id ).all | |
----- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment