Last active
December 18, 2015 15:28
-
-
Save tkawa/5804142 to your computer and use it in GitHub Desktop.
Nested Resource's parent
by @moro https://speakerdeck.com/moro/concerning-application
This file contains hidden or 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
# config/routes.rb | |
# question and answers that the user has | |
# /users/:user_id/questions | |
# /users/:user_id/answers | |
resources :users do | |
resources :questions | |
resources :answers | |
end |
This file contains hidden or 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
# app/controllers/concerns/users_resource.rb | |
module UsersResource | |
extend ActiveSupport::Concern | |
included do | |
helper_method :owner | |
hide_action :owner | |
end | |
private | |
def owner | |
@_owner ||= | |
User.where(id: params[:user_id]).first | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
インスタンス変数よりメソッドのほうが自然、ということですかねぇ。僕もどちらかというとメソッドのほうが好きかなぁ。
hide_action
は要らないんじゃないかと思いますがよくわかりません…。ひょっとするとinclude先では扱いが違うのかも。