它是隐藏在私有方法的实例变量初始化一个好的做法呢?
例如,我有一些动作的用户控制:
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
有人说,它看起来像一个神奇的,但它是干的。 你怎么看?