在私有方法中隐藏实例变量初始化是一个好习惯吗?
例如,我有一个带有一些操作的用户控制器:
class UsersController < ApplicationController
before_filter :get_user, only: [:show, :edit, :update, :destroy]
before_filter :set_user, only: [:new, :create]
def index
@users = User.all
end
def show
end
def new
end
def edit
end
def create
if @user.save
redirect_to @user, notice: 'User was successfully created.'
else
render action: 'new'
end
end
def update
if @user.update_attributes(params[:user])
redirect_to @user, notice: 'User was successfully updated.'
else
render action: 'edit'
end
end
def destroy
@user.destroy
redirect_to users_path
end
private
def get_user
@user = User.find(params[:id])
end
def set_user
@user = User.new(params[:user])
end
end
有人说它看起来像一个魔法,但它是干燥的。你怎么看?