문제

What is the reason for using a before_filter :load?

def load
  @posts = Post.all
  @post = Post.new
end

What does this accomplish? I've just seen it done in a tutorial and don't understand if it's beneficial.

도움이 되었습니까?

해결책

In this case the load method will be called for all method calls within the controller. Meaning @posts and @post will be available to all actions for said controller. It is very rare that I use them. Just calling load within the action that needs it and moving the load method to private should be good enough.

If you wanted @posts and @post to be available for all the actions in the controller then this is an acceptable solution.

You could always do before_filter :load, only: [:index]

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top