Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
使用 a 的原因是before_filter :load什么?
before_filter :load
def load @posts = Post.all @post = Post.new end
这有什么作用?我刚刚在教程中看到了它,不明白它是否有益。
在这种情况下,将为控制器内的所有方法调用调用 load 方法。含义@posts并将@post可用于所述控制器的所有操作。我很少使用它们。只需在需要它的操作中调用load并将加载方法移动到私有就足够了。
@posts
@post
load
如果您想要@posts并且@post可用于控制器中的所有操作,那么这是一个可以接受的解决方案。
你总是可以做before_filter :load, only: [:index]
before_filter :load, only: [:index]