给定以下控制器方法:
def create
@client = User.find(params[:client][:id])
respond_to do |format|
if @client.present? && @client.add_manager(current_user)
format.html { redirect_to clients_path, notice: "Successfully added manager" }
else
format.html { redirect_to clients_path, error: "Could not find client" }
end
end
end
如何让它在 else 块中正确失败,而不是在生产中抛出 RuntimeError 变成“出现问题”?
def add_manager(user)
raise "Already a manager" if self.manager_users.include?(user)
self.manager_users << user if user.trainer?
end
是代码吗...