Skip to content

Instantly share code, notes, and snippets.

@shu0115
Created March 27, 2012 03:03
Show Gist options
  • Save shu0115/2212130 to your computer and use it in GitHub Desktop.
Save shu0115/2212130 to your computer and use it in GitHub Desktop.
controllerとviewの両方からメソッドを呼びたい場合
◆コントローラに書いたメソッドを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