在私有方法中隱藏實例變量初始化是一種好的做法嗎?before_filter方法中隱藏的實例變量
例如,我有一些動作的用戶控制器:
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
有人說,它看起來像一個神奇的,但它是乾的。你怎麼看?