如果我生成一个脚手架,我会得到标准动作索引,new,show,create ....所有这些都包含一行,例如喜欢
@comment = Comment.find(params[:id])
将此行以单独的方法放入控制器(如
)是否有意义def load
@comment = Comment.find(params[:id])
end
这是一个优势吗?感谢你的时间
答案 0 :(得分:2)
是单独的方法,是的,使用before_filter也是。
class CommentsController < ApplicationController
# Whitelist your before_filter like this. Or you can blacklist it with :except => []
before_filter :load_comment, :only => [:edit, :update, :destroy, :show]
def show
end
def index
@comments = Comment.all
end
def new
@comment = Comment.new
end
# etc ...
protected
def load_comment
@comment = Comment.find(params[:id])
end
end